I want to see if a button was clicked. If it was clicked, return a alert outside of the click function. Here’s my code:
$(document).ready(function() {
var test = 0;
$('body').on('click', '#publish', function() {
test = 1;
});
if(test == 1) {
alert('jay!');
}
});
Not it doesn’t show the alert window upon click. Why? Do I have to use the if statement inside the click function? I want this code to work as it is.
http://jsfiddle.net/edgren/TXERr/
Thanks in advance.
your code can’t work because the
ifstatement is executed before the click event. (and as sidenote: as a good practice always use triple equality in your comparison)Do instead