Skip to content

Instantly share code, notes, and snippets.

@paraggupta1993
Last active August 29, 2015 14:04
Show Gist options
  • Save paraggupta1993/dd2c92437b51677a3564 to your computer and use it in GitHub Desktop.
Save paraggupta1993/dd2c92437b51677a3564 to your computer and use it in GitHub Desktop.
teminateEvent : Stops bubbling-up and propogation of events in EventHandlers.
terminateEvent = function(event){
event.stopPropagation(); // Stops Bubbling up to Ancestor elements
event.preventDefault(); // Stops the default action Eg : Redirecting to a "href" for anchor element
return false;
}
$("#anchor-element").click( function(event){
//Do something
return terminateEvent();
});
//------------------------------------------------------------------------------------
/* Example =>
http://jsfiddle.net/4b64z/2/
<!--- HTML --->
<div id="parent-element">
<a href="www.google.com" id="element1"> Button1 </a>
<a href="www.google.com" id="element2" onclick="func2(event)"> Button2 </a>
</div>
// JAVASCRIPT
func = function(event){
// DO something
return terminateEvent(event);
}
func2 = function(e){
return terminateEvent(e);
}
terminateEvent = function(event){
event.stopPropagation();
event.preventDefault();
return false;
}
$("#tagid").click(func);
$("#parent-element").click(function(event){ console.log("Parent-element Clicked"); });
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment