InterviewHubPRO
Section Overview
Q108

Question 108 of 521

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

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

What is the use of stopPropagation method

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>