Skip to content

Instantly share code, notes, and snippets.

@dodalovic
Created August 23, 2014 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dodalovic/2806358cb74d25659cda to your computer and use it in GitHub Desktop.
Save dodalovic/2806358cb74d25659cda to your computer and use it in GitHub Desktop.
Prevent event bubbling
'use strict'
var eventDetails = function(element, event) {
event.stopPropagation ? event.stopPropagation() : (event.cancelBubble = true);
console.log("target.id:" + event.target.id + ", this.id:" + element.id);
};
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="bubbling/js/bubbling.js"></script>
</head>
<body>
<div id="1" onclick="eventDetails(this, event);">
<p id="d1-p">some 1 content</p>
<div id="2" onclick="eventDetails(this, event);">
<p id="d2-p">some 2 content</p>
<div id="3" onclick="eventDetails(this, event);">
<p id="d3-p">some 3 content</p>
</div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment