Counter javascript/html

Asked

Viewed 14,186 times

0

I need to make an application in javascript/jquery/html that counts the user’s clicks on the button and when the counter reaches 10 lock the button and displays an Alert, I don’t know why it’s not displaying. Does 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> <!-- add Jquery CDN -->

    <link href src="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> <!-- add Bootstrap CDN -->

    <link href="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>

  • 3

    var count = 0; out of function contador() and it should already work. The problem is that you are putting the count = 0 every time you called her

1 answer

6

Your Count 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 to 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> <!-- add Jquery CDN -->

    <link href src="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> <!-- add Bootstrap CDN -->

    <link href="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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.