Skip to main content
InterviewHubPRO
Section Overview
Q108

Question 108 of 536

Part 3: DOM, Event Flow, Timers & Browser APIs (104 - 173)

20%
JavaScript & V8 Internals (536 Qs)•Part 3: DOM, Event Flow, Timers & Browser APIs (104 - 173)

What is the use of stopPropagation method

junior LevelCode ExampleHTTP & REST APIs

The stopPropagation method is used to stop the event from bubbling up the event chain. For example, the below nested divs with stopPropagation method prevents default event propagation when clicking on nested div(Div1)

javascript
  <p>Click DIV1 Element</p>
  <div onclick="secondFunc()">DIV 2
    <div onclick="firstFunc(event)">DIV 1</div>
  </div>

  <script>
  function firstFunc(event) {
    alert("DIV 1");
    event.stopPropagation();
  }

  function secondFunc() {
    alert("DIV 2");
  }
  </script>