Ask Your Question
0

When using addEventListener in React, why does the onclick event listener of an outer element get triggered prior to the onclick event listener of an inner element?

asked 2021-08-25 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-05-31 18:00:00 +0000

devzero gravatar image

This is because of the concept of event bubbling in the DOM (Document Object Model). When an event is triggered on an element, the event first goes through the innermost element and then propagates outwards to the outer elements, triggering their event listeners as well. This is known as event bubbling.

So, when we have an inner element and an outer element with their respective event listeners, the event on the inner element will be triggered first and then the event on the outer element will be triggered. This behavior can be changed using stopPropagation() method to prevent this bubbling behavior.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-25 11:00:00 +0000

Seen: 12 times

Last updated: May 31 '22