← Back to jQuery Course | Chapter 3: Events | Lesson 8 of 11

Event Propagation and Bubbling

Events normally move from the target toward its parent elements.

Bubbling

An event fired on a child element travels upward through its ancestor elements unless something stops it -- this upward travel is called bubbling.

Example: Bubbling

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="outer"><button id="inner">Click</button></div>
    <script>
      $("#outer").on("click", function() { console.log("Outer handler ran"); });
      $("#inner").on("click", function() { console.log("Inner handler ran"); });
    </script>
  </body>
</html>

Stop Propagation

Calling event.stopPropagation() inside a handler stops the event from continuing upward, so any handlers attached to parent elements for that same event never run.

Example: Stop Propagation

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="outer"><button id="inner">Click</button></div>
    <script>
      $("#outer").on("click", function() { console.log("Outer handler ran"); });
      $("#inner").on("click", function(event) {
      event.stopPropagation();
      console.log("Inner handler ran, stopped bubbling");
      });
    </script>
  </body>
</html>

Parent and Child

Because of bubbling, both a child element's own handler and any handler on its ancestors can respond to the same single click, running in order from the innermost element outward.

Example: Parent and Child

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="card"><button id="btn">Click</button></div>
    <script>
      $("#btn").on("click", function() { console.log("1: button handler"); });
      $("#card").on("click", function() { console.log("2: card handler"); });
    </script>
  </body>
</html>

Prevent Bubbling

stopPropagation() is the tool to reach for specifically when a child element's own action -- like a button inside a clickable card -- shouldn't also trigger whatever the card's own click handler does.

Example: Prevent Bubbling

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="card">Card <button id="btn">Delete</button></div>
    <script>
      $("#card").on("click", function() { console.log("Card clicked"); });
      $("#btn").on("click", function(event) {
      event.stopPropagation();
      console.log("Button deleted, card click suppressed");
      });
    </script>
  </body>
</html>

Delegated Events

Event delegation deliberately relies on bubbling rather than fighting it: instead of attaching a handler to every child, you attach one handler to a shared parent and use the bubbled event's target to figure out which child was actually interacted with.

Example: Delegated Events

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <ul id="list"><li>Item 1</li></ul>
    <script>
      $("#list").on("click", "li", function(event) {
      console.log("Clicked:", event.target.textContent);
      });
    </script>
  </body>
</html>

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.