Avoid return false in jQuery Click Bindings
If you’ve ever written any jQuery code to bind to a click event, you’ve likely written something of the form:
$("a.special").click(function(){
// do stuff
return false;
});
OR
$("a.special").bind("click", function(){
// do stuff
return false;
});
In these cases, the click event fires and causes the function to do some stuff. The return false prevents the normal behavior of the anchor, i.e. the href attribute is never executed (new URL, hash, etc.). This might appear to be fine on the front-end, but the return false breaks the bubbling of additional click events. This means if there are any other click bindings, after this function gets called, those others do not. Consider this example:
$("a.modal").click(function(){
openModal( $(this).attr("href") );
return false;
});
$("a").click() {
trackCustomAnalytics("click", $(this).attr("href"));
}
If the first function above fires first, the return false prevents the generic all-anchors analytics function from firing. This is easily preventable by avoiding return false. But you might think “how do I prevent the href on the anchor from executing?” You can use the preventDefault() method of the event to handle this. The appropriate updated code to do such a thing would look like this:
$("a.modal").click(function(e){
e.preventDefault();
openModal( $(this).attr("href") );
});
$("a").click(e) {
e.preventDefault();
trackCustomAnalytics("click", $(this).attr("href"));
}
As you can see, each function has a parameter passed in, the event object. You can call preventDefault() on this parameter and it will prevent the default behavior of the event, which in the case of a click is the execution of the href attribute.
Read More
Read more about event.preventDefault()
2 Comments + Add Comment
Got anything to say? Go ahead and leave a comment!
Recent Comments
- Performance tuning your Sitecore installation | Agile and ALM: Software development today on A Going Live Checklist for Sitecore Websites
- Imran Saleem on Sitecore Avanced Database Crawler Occasionally Provides Null Results
- Ty Cahill on Sitecore Front-End Development Best Practices
- Sitecore Managed Sites as Virtual Folders | Fire Breaks Ice on Sitecore Item and Field Names
- Krimos on Using the DataSource Field with Sitecore Sublayouts
Sitecore Links
- .Sitecore
- Aboo Bolaky
- Alex Shyba
- Anders Dreyer
- aweber1.0
- Brian Pedersen
- Christopher Wojciech
- Coffee => Coder => Code
- Dev Sitecored²
- Everything Web
- Image0.com blog
- John West
- Learn Sitecore
- Let's do Sitecore
- Mark van Aalst
- Matthew Kenny
- Molten Core
- Project Lifecycle
- Sean Kearney
- Sebastian Patten
- Sitecore Australia
- Sitecore Blog
- Sitecore Climber
- Sitecore Development
- Sitecore Gadgets
- Techphoria414
- The Client View
- The Sitecore Experience
- Web Content Management and Delivery
Archives
- April 2013 (1)
- February 2013 (1)
- January 2013 (1)
- December 2012 (1)
- June 2012 (2)
- May 2012 (2)
- March 2012 (1)
- February 2012 (1)
- January 2012 (5)
- December 2011 (4)
- November 2011 (1)
- July 2011 (1)
- June 2011 (1)
- May 2011 (2)
- March 2011 (6)
- February 2011 (2)
- January 2011 (10)

Posted under:
[...] see it in use, here’s a hard-coded inline example. (Don’t forget, the return false in a JavaScript click event is [...]
Good observation. Thanks.