questions5 Flashcards
[Bubbling] How should we go if we want that any new additional element in the dom tree has a click handler?
We have two variants, add the EventListener directly on a newly created element or with event delegation.
event delegation
It`s a pattern when instead of assigning EventListeners on each element we are assigning it to the common ancestor element. In the handler, we are getting e.target to check where the event has actually happened and handle it.
[Capturing] In which order will these handlers be invoked? How could we change this order?
document.querySelector(‘body’).addEventListener(‘click’, callback1); document.querySelector(‘button’).addEventListener(‘click’, callback2);
Button first then Body. We need to give a 3rd parameter true to tern on the Capturing so the Body will be first and then the button
StopPropagation()
event.stopPropagation() stops the move upwards, but on the current element all other handlers will run.
To stop the bubbling and prevent handlers on the current element from running, there’s a method event.stopImmediatePropagation(). After it no other handlers execute.
PreventDefault()
gives you the ability to prevent a browser’s default behavior for events.
for example, when you click on a checkbox it’ll toggle the check. However, if you use preventDefault it will actually stop the browser from doing that.
You can use preventDefault on all sorts of different HTML elements.
There are some events that the browser can’t prevent from happening. For example, events like scroll and wheel (for mouse wheel events!)
Target and Current Target difference
event. target – the deepest element that originated the event.
event. currentTarget (=this) – the current element that handles the event (the one that has the handler on it)
bubbling
events “bubble” from the inner element up through parents
event.target
e.target is the actual element inside the form that was clicked.
this – is the “current” element, the one that has a currently running handler on it.