javascript - Variable value check isn't working -


i have <div> i'm clicking on change value of variable. variable changes if(oneorzero == 0) never works. continues == 0. though variable different, still acts same. doing wrong?

edit:

i left part out. after change variable, need check onmousedown function see if worked. issue.

var oneorzero = 0;  var onmousedownfunction= function() { if(oneorzero == 0){    alert('one or 0 equals 0');    //do }; if(oneorzero == 1){    alert('one or 0 equals 1');    //do }; };  $('.class').click(function(){     val_class = $(this).attr('value');     if(val_class == '0'){         $(this).attr('value','1');         oneorzero = 1;     };     if(val_class == '1'){         $(this).attr('value','0');         oneorzero = 0;     }; });  $(canvas).mousedown(onmousedownfunction); 

place inside click event.

the way have written execute once when page loads.

also use console.log instead of alert lot more elegant;

also can use parseint , eliminate variable using it

$('.class').click(function () {     var val_class = parseint($(this).attr('value'), 10),         oneorzero;      if (val_class === 0) {         oneorzero = 1;      } else if (val_class == 1) {         oneorzero = 0;     }     $(this).attr('value', oneorzero );     console.log(val_class); }); 

check fiddle

the same can written in single line , can eliminate if else using ternary operators

$('.class').click(function () {     var $this = $(this),         val_class = parseint($(this).attr('value'), 10) === 0 ? 1 : 0 ;     $(this).attr('value', val_class).text(val_class);      console.log(val_class); }).click(); 

another fiddle

mousedown fiddle


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -