JS Coding Questions Logo
JS Coding Questions
#104💼 Interview💻 Code

What is the use of stopPropagation method

Advertisement

728x90

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
1<p>Click DIV1 Element</p>
2  <div onclick="secondFunc()">DIV 2
3  <div onclick="firstFunc(event)">DIV 1</div>
4  </div>
5
6  <script>
7
8function firstFunc(event) {
9  alert("DIV 1");
10  event.stopPropagation();
11}
12
13function secondFunc() {
14  alert("DIV 2");
15}
16</script>

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 19

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
104of476