Event Bubbling in Javascript

Today, while creating some javascript popup I had a condition in which I place onclick function on body tag. I had an other button which also implement onclick. So the code looks like as follows

   1: <body onclick="disappearPopup()">
   2: <a href="#" onclick="ShowPopup()">Show me</a>
   3: </body>

So, what happen is whenever link is click the body clicked even called automatically. Practically speaking, we need to cancel the body onclick even each time we click the link click.

And for that I write the following code on click of the link

   1: if (window.event) {
   2:     window.event.cancelBubble = true;
   3: }
   4: else {
   5:     e.stopPropagation();
   6: }

And it works ……..

Published Friday, January 30, 2009 3:11 PM by aghausman12
Filed under:

Comments

# re: Event Bubbling in Javascript

Thursday, February 05, 2009 4:59 PM by tsantos

That's really useful. Thank's a lot for sharing.

Leave a Comment

(required) 
(required) 
(optional)
(required)