Middle
Is it possible to stop event propagation? How? What is the difference between stopPropagation and stopImmediatePropagation?
sobes.tech AI
Answer from AI
Yes, event propagation can be stopped using the stopPropagation() and stopImmediatePropagation() methods of the event object.
event.stopPropagation()prevents the event from bubbling up the DOM tree, but other handlers on the current element will still be called.event.stopImmediatePropagation()not only stops the event from bubbling but also prevents other handlers of the same event on the current element from executing.
Example:
element.addEventListener('click', (event) => {
event.stopPropagation(); // or event.stopImmediatePropagation();
console.log('Handler on the element');
});