javascript / html counter

0

I need to make an application in javascript / jquery / html that counts the clicks of the user on the button and when the counter reaches 10 it locks the button and displays an alert, I do not know why it is not displaying. Anyone know?

<head>

    <title>Página</title> <!-- Add page title -->

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><!--addJqueryCDN--><linkhrefsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" media="screen"></script> <!-- add bootstrap css CDN --> 
</head>


<body>


    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><!--addBootstrapCDN--><linkhref="design.css" rel="stylesheet" medi="screen"> <!-- Add css in html -->


    <div class="container">
        <div class="row">
            <div class="col-lg-12 tabela">
                linha azul

            </div>
        </div>
    </div>



    <script type="text/javascript"> // all javascript/jquery code start here

        function add_field() 
    {
        var form = document.getElementsByTagName('form')[0],
        input = document.createElement('textarea');
        input.setAttribute('type', 'textarea');
        input.setAttribute('name', 'item');
        form.appendChild(input);

    };


                function contador() 
    {
        var count = 0;
        count++;

        if(count == 10){
            alert("QWERTY");

        }

    };



    </script>


        <form name="input" method="get">
            <div class="ui-input-text">      
                <div data-role="navbar">
                      <button type="button" onclick="add_field(); contador();">ADD</button><br>
                </div>
            </div>
        </form>


</body>

    
asked by anonymous 23.09.2017 / 21:35

1 answer

6

Your account in this way will always be 0:

function contador() 
    {
        var count = 0;
        count++;

        if(count == 10){
            alert("QWERTY");

        }

    };

then the correct would be you declare the cont outside the function contador()

   var count = 0;   
 function contador() 
        {

            count++;

            if(count == 10){
                alert("QWERTY");
             document.getElementById('btn').disabled=true;

            }

        };

See your code with the change made. put an id for the button:

function add_field() 
    {
        var form = document.getElementsByTagName('form')[0],
        input = document.createElement('textarea');
        input.setAttribute('type', 'textarea');
        input.setAttribute('name', 'item');
        form.appendChild(input);

    };
    
var count = 0;

function contador() 
    {
        
        count++;

        if(count == 10){
            alert("QWERTY");
   document.getElementById('btn').disabled=true;

        }

    };
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><!--addJqueryCDN--><linkhrefsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" media="screen">



    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><!--addBootstrapCDN--><linkhref="design.css" rel="stylesheet" medi="screen"> <!-- Add css in html -->


    <div class="container">
        <div class="row">
            <div class="col-lg-12 tabela">
                linha azul

            </div>
        </div>
    </div>




        <form name="input" method="get">
            <div class="ui-input-text">      
                <div data-role="navbar">
                      <button type="button" onclick="add_field(); contador();" id="btn">ADD</button><br>
                </div>
            </div>
        </form>


</body>
    
23.09.2017 / 21:45