I am stuck in this code and donβt understand why it does not work as I expect. Thus, the boolean variable "x" changes its value each time I press the button #btn:
$(document).ready(function() {
var x = false;
$("#btn").click(function() {
toggleBtn(x);
x = !x;
});
function toggleBtn(x) {
if (!x) {
doThis();
} else {
doThat();
}
};
});
But in this way, "x" does not change its meaning:
$(document).ready(function() {
var x = false;
$("#btn").click(function() {
toggleBtn(x);
});
function toggleBtn(x) {
if (!x) {
doThis();
} else {
doThat();
}
x = !x;
};
});
Why is this?
source
share