We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

noah on 06/06/07


Tagged

DOM events eventhandlers ppk interaction bubbing quirksmode bubble propagation


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

inamorix


Stop event propagation (stop an event from bubbling up)


Published in: JavaScript 


This is useful if I have an element that is inside a container, and I want to the element's event handler to fire without triggering the container's event handler. Then I would wrap the element's event handler function with this code.

See "PPK On JavaScript," pp. 315 and 323.

  1. function doSomething (e) {
  2. var event = e || window.event;
  3.  
  4. [body of event handler function goes here]
  5.  
  6. if (event.stopPropagation) {
  7. event.stopPropagation();
  8. } else {
  9. event.cancelBubble = true;
  10. }
  11. }

Report this snippet 

You need to login to post a comment.