Check number of clicks on buttons

Asked

Viewed 1,733 times

1

I have a script with functions to control what each button of an application does. I declared a global variable so that when all buttons are clicked, two actions are triggered: hide a div and show another.

//variável global

var completo = 0;

//funções para controlar 9 botões - em cada uma das funções incremento a variável:

function A()
{
     faz qualquer coisa;
     completo++
}
function B()
{
     faz qualquer coisa;
     completo++
} 
function C()
{
     faz qualquer coisa;
     completo++
} 
function D()
{
     faz qualquer coisa;
     completo++
} 
function E()
{
     faz qualquer coisa;
     completo++
} 
function F()
{
     faz qualquer coisa;
     completo++
} 
function G()
{
     faz qualquer coisa;
     completo++
}
function H()
{
     faz qualquer coisa;
     completo++
} 
function I()
{
     faz qualquer coisa;
     completo++
}

So far it should increment 9 times the complete variable, being complete=9, right?

Then it would trigger action - when the variable reaches 9, hide the general div and show final div.

if(completo==9)
{
    $('div[id^="final"]').show();
    $('div[id^="geral"]').hide();
}

this last part is not functional.

also tested with the following, but without success:

if(completo==9)
{
    $("#final").show();
    $("#geral").hide();
}

can help me?

In the html part I have declared both the general div and the final div:

<div id="geral">"conteudo blablabla"</div>
<div id="final">"conteudo blablabla"</div>
  • What is the code that calls these functions A, B, C, ...? I don’t see where these functions are called and the completo is always zero...

  • The code of the button that calls the contents of the function is: <td><input type="button" class="astext" value="D" onclick="A()"/></td>

  • Okay, so within each function you have to call another function that has the if inside. If not this if is only checked when the page loads and never again. It makes sense?

  • Aside from the problem @Sergio pointed out, this all assumes that each button can only be pressed once, right? Because if you allow more than one click per button, your logic will not work (for example, press 9 times a single button to meet the condition).

  • bfavaretto, correct, that part will still be evaluated below. For now I am committed to putting this functional! : ) Sergio, thanks for the tip, excellent!

1 answer

1

What Sergio said was right... Remember that JS is only loaded once on your page, so it should have a function that checks if your global variable has reached the 9 events of click...


Js Puro:

function A(){
    teste();
}

function B(){
    teste();
}

function C(){
    teste();
}

function D(){
    teste();
}

function E(){
    completo++;
    teste();
}

function F(){
    teste();
}

function G(){
    teste();
}

function H(){
    teste();
}

function I(){
    teste();
}

function teste(){
    completo++;

    if(completo==9){
        document.getElementById("geral").style.visibility = "hidden";
        document.getElementById("final").style.visibility = "visible";
    }
}

Another solution would be to take advantage of the JQuery used in your page and put the function that checks the variable "global" inside the ready page, thus:


Jquery

var completo = 0;
$(document).ready(function(){

    $(":button").click(function() {
        completo++;
        $(this).hide();

        if(completo==9){
            $('div[id^="final"]').show();
            $('div[id^="geral"]').hide();
        }
    });
});

Using this second implementation you can hide the button that triggered the click with that line: $(this).hide();

  • Marcelo, thank you so much! However I am not getting that, at the 9, hide one div and show the other :/ Sorry for some questions, I’m still starting...

  • edited the @lfprata code, with a function that hides the button that was clicked, an alternative would just leave the button disabled

  • @Marcelobonifazio correct, now yes, already does something! Thank you. I will however have to change the property of the div "final" since you put also as Hidden - the idea would be to show this, correct?

  • @lfprata now that you came up with this doubt, it seems that I understood what was the real problem, check if you are referencing the library of JQuery... the functions $('div[id^="final"]').show(); and $('div[id^="geral"]').hide(); need the library of the JQuery to work

Browser other questions tagged

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