Variable control with Javascript for html

Asked

Viewed 239 times

3

I have an HTML code with Javascript that when clicking above; If x == 1, performs task A and variable javascript arrow x = 0, otherwise, performs task B and variable javascript arrow x = 1, but the variable is reset every time you start the function.

How to solve ?

Javascript:

function toogle_m(MID, PID) {
    if(x == 1) {
        alert("Luck 7!"+"x");
        var x = 0;
    } else {
        alert("You're not very locky today..."+"x");
        var x = 1;
    }

    $(MID).fadeToggle(500);
    $(PID).hide();
}

HTML:

<div class="F1" onclick="toogle_m(del)">
    <img src='resources/images/andremachado/del.png' width='25' title='Excluir insumos'/>
</div>

I know it will restart because the function is started always, but what the solution ?

  • has already tried to transform the variable into global ?

  • 1

    Recommended Reading http://answall.com/questions/2513/quando-se-deve-usar-var-no-javascript

2 answers

5


Your problem lies in the scope in which the variable was declared. If you declare a variable within the function, its lifespan will be equivalent to that of the function. One way around this is by declaring the variable outside the function, it will look like this:

// Inicializa com 0.
var x = 0;

function toogle_m(MID, PID) {
    if(x == 1) {
        alert("Luck 7!"+"x");
        x = 0;
    } else {
        alert("You're not very locky today..."+"x");
        x = 1;
    }

    $(MID).fadeToggle(500);
    $(PID).hide();
}
  • I tried how you spoke and did not, I also made another code with the same thought and only returns the first if: var foo = 0; Function toogle_m(MID,PID){ if (foo =0) { Alert("Lucky 7!"+"x"); var foo = 0; } Else{ Alert("You’re not very Lucky Today..."+"x"); var foo = 1; } $( MID ).fadeToggle(500); $( PID ).Hide(); }

  • @Andrésilveiramachado sure you did it right? why the code you posted here in the comment is not right, pay attention to the response code, when you enter the if i declare the value to the variable x declared outside the function, in the commented example you create a new variable and assign the value, not assigned to the external variable.

  • 1

    Thank you, I removed the var from the side and it worked, sorry, your code was correct!

3

Try this by making X your global variable.

var foo = 0;
function toogle_m(){
    if (foo == 1) { 
        alert("Lucky 7!"+"x");
    foo = 0;
  }
  else{
    alert("You're not very lucky today..."+"x"); 
    foo = 1;
  }

}

$(document).ready(function(){
    $('.F1').click(function(){
      toogle_m();      
    });
});

Html

<div class="F1">
    <img src='resources/images/andremachado/del.png' width='25' title='Excluir insumos'/>
</div>

Example

jsFiddler

  • I tried how you spoke and did not, I also made another code with the same thought and only returns the first if: var foo = 0; Function toogle_m(MID,PID){ if (foo =0) { Alert("Lucky 7!"+"x"); var foo = 0; } Else{ Alert("You’re not very Lucky Today..."+"x"); var foo = 1; } $( MID ).fadeToggle(500); $( PID ).Hide(); }

  • stops using var foo everywhere, leaves var foo overall and then within the method use only foo, without var. http://jsfiddle.net/rboschini/tbbb5d2L/2/

  • 1

    Thank you, I took the var off the side and it worked!

  • From joinha and puts my Ref as correct, this helps me with the score!" ;)

Browser other questions tagged

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