I’m using Firefox and JQuery:
The following code hides the content briefly, but then the content reappears:
$("#calorielink").click(function() {
$("#A").hide();
$("#B").hide();
});
The following code CORRECTLY hides the content:
$("#calorielink").click(function() {
$("#A").hide();
$("#B").hide();
return false;
});
Why does the return statement have an effect?
return falsedoes the following things for you in an event handler:#calorieLinkis ana, this means the user is not directed to a new page.Usually, you just want the first one (you only need to prevent the default action):
I would generally recommend against using
return falsein event handlers. Think about what you want to happen in your event handler, and even if you do need both of the things thatreturn falseaccomplishes, it’s more expressive to write:Here’s a good read about what
return falseis actually doing.Not including
return falseor stopping the events default action (event.preventDefault()) will cause your code to run and the default behavior of the event will occur as well. This is probably why you’re seeing the code run quickly and then the effects disappear.