JS Events
In this page:
element.addEventListener("eventName", function(event) {
// handler code
});
<element onclick="handler()">
onclick
onclick fires whenever an element is clicked, and can be set either as an inline HTML attribute or assigned directly in JavaScript to run a specific function.
Example: onclick
<button onclick="console.log('Clicked!')">Click Me</button>
onmouseover
onmouseover fires when the mouse pointer enters an element's boundaries, commonly paired with onmouseout, which fires when the pointer leaves, to create hover-based effects.
Example: onmouseover
<div onmouseover="console.log('Mouse entered')" onmouseout="console.log('Mouse left')">Hover over me</div>
onchange
onchange fires when a form element's value has been modified and then loses focus, commonly used with dropdowns, checkboxes, and text inputs to react to user selections.
Example: onchange
<!-- The onchange attribute below runs this JS whenever the selection changes -->
<select onchange="console.log('Selection changed to', this.value)">
<option value="a">A</option>
<option value="b">B</option>
</select>
addEventListener
addEventListener attaches an event handler to an element without overwriting any existing handlers, and lets you attach multiple listeners for the same event, making it the modern, recommended way to handle events.
Example: addEventListener
<button>Click Me</button>
<script>
// Declare the constant `btn`, set to `document.querySelector("button")`
const btn = document.querySelector("button");
// Listen for the `click` event on `btn` and run the handler when it fires
btn.addEventListener("click", () => console.log("First listener"));
// Listen for the `click` event on `btn` and run the handler when it fires
btn.addEventListener("click", () => console.log("Second listener"));
</script>
The Event Object
Every event handler automatically receives an event object as its first argument, containing details about what happened, including which element triggered it, mouse coordinates, or which key was pressed.
Example: The Event Object
<button onclick="console.log(event.target)">Click Me</button>
- Relying only on inline event attributes like onclick for complex applications, addEventListener scales far better and allows multiple handlers.
- Forgetting that the event object passed to a handler contains useful details, like which key was pressed or which element was clicked.
- Not understanding event bubbling, which can cause a click handler on a parent element to fire unexpectedly when a child element is clicked.
- Events let JavaScript respond to user actions like clicks, mouse movement, and form changes.
- addEventListener is the modern, flexible way to attach event handlers, supporting multiple listeners per element.
- Events bubble upward through parent elements by default, a behavior worth understanding before building complex interactions.
Standard DOM events and addEventListener are supported in every modern browser, forming the foundation of nearly all interactive web pages.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: