Difference in core javascript and jquery event handling
I was surprised to see that jquery handles events in a different way as compared to core javascript event handling.
Here is the case
jQuery("#testAnchor").click(function() {
//do some thing
return false;
});
If you are returning false in an event handler in jquery, that means you are stopping propagation and preventing default. All the handlers listening on the click event on that element will be called and the event will not be propagated to the parent element.
If you use
document.getElementById("testAnchor").onclick = function(event) {
event = event || window.event;
//do some thing
return false;
returning false in this case wont stop propagating the event. we should indeed say event.cancelBubble= true or event.stopPropagation() based on the browser.
Here is the case
jQuery("#testAnchor").click(function() {
//do some thing
return false;
});
If you are returning false in an event handler in jquery, that means you are stopping propagation and preventing default. All the handlers listening on the click event on that element will be called and the event will not be propagated to the parent element.
If you use
document.getElementById("testAnchor").onclick = function(event) {
event = event || window.event;
//do some thing
return false;
returning false in this case wont stop propagating the event. we should indeed say event.cancelBubble= true or event.stopPropagation() based on the browser.
Comments