A browser event is a signal that something has happened. All DOM nodes generate such signals (but events are not limited to DOM).
Examples:
Mouse: click, contextmenu, mousemove
Keyboard: keydown, keyup
Form: submit, focus
Document: DOMContentLoaded (DOM is fully built)
CSS: transitionend (CSS animation finishes)
Event Handler
1. HTML attribute
The onclick value which is string
It is internally wrapped inside another function and then called.
<input value="Click me" onclick="alert('Click!')" type="button"><button onclick="alert(this.innerHTML)">Click me</button><input value="Click me" onclick="alert(event.type)" type="button"><!-- Handler internally will be same as:input.onclick = function(event) { alert(event.type);}-->
2. DOM property
Get the HTML node and assign handler
Use onclick not onClick since DOM properties are case sensitive
Using methods we can add multiple events to the same node
This should be the preferred way to add event handlers
function handler() { alert('Thanks!');}elem.addEventListener("click", handler);// remove handler (requires same fn reference)elem.removeEventListener("click", handler);
Event properties
event.type: for example click
event.clientX, event.clientY: relative coords of pointer
event.currentTarget: same as this
event.target:
element which initiated the event.
It does not change on bubbling process
most deeply nested element that caused the event
this
It is element itself and same as currentTarget
<div id="outer"> <h1>Hello World!</h1> <p>This is a paragraph.</p></div><script> outer.addEventListener('click', function(e) { alert(`target = ${e.target.tagName}, this = ${this.tagName}, currentTarget = ${e.currentTarget.tagName}`); // target = H1, this = DIV, currentTarget = DIV // target = P, this = DIV, currentTarget = DIV })</script>
Event methods
event.stopPropagation(): stops propagating upwards but all handlers run on current element
event.stopImmediatePropagation(): stops propagating upwards and no other handler runs
event.preventDefault(): prevent default action by browser
Default action
event.preventDefault(): prevent default action by browser after handler is executed
For <a> tag it is navigation to new url
For <button> on form it is submit action
event.defaultPrevented is true if default action was prevented
For event types with pattern: on<EventName> we can also return false to prevent default action
Event Phases
Capturing Phase - Event goes down the element. Traverses the tree to find the target element
Target Phase - Event reached the target element
Bubbling Phase - Event bubbles up from the element
Event Bubbling
When an event happens on an element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors
This bubbling occurs up the element’s ancestors all the way to the document
Event bubbling is the mechanism behind event delegation
Almost all events bubble. For instance focus doesn’t bubble
Event Delegation
The idea is that if we have a lot of elements handled in a similar way, then instead of assigning a handler to each of them – we put a single handler on their common ancestor.
This pattern is based on bubbling and capturing
We put event handler on ancestor and determine who initiated the event using event.target