the function is triggered via a button so I thought every time you push it will check x set the background accordingly and put x in the other state so the next push gives the other result
I just saw that I can use the .css in an .toggle but I'm still curious why my solution wont work
var x = 0;
function toggledark(x){
if (x == 0) {
$('body').css({'background-color': 'red'});
x+=1;
return x;
}
else {
$('body').css({'background-color': 'black'});
x-=1;
return x;
}
}
I thought it will toggle but I only get black and it stays this way
xintoggledark(x)is shadowing the global variablex.event, so you're just renamingeventasx. Just remove thexfrom your parameters, and you should be golden as is. jsfiddle.net/ntdh8v1w