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 ?
– Otto
Recommended Reading http://answall.com/questions/2513/quando-se-deve-usar-var-no-javascript
– Guilherme Lautert