JS Events Advanced
In this page:
element.addEventListener("eventName", handler, options);
element.removeEventListener("eventName", handler);
Event Delegation
Event delegation relies on event bubbling, attaching a single listener to a parent element and using the event object's target property to determine exactly which child element actually triggered it, rather than attaching a separate listener to every child.
उदाहरण: Event Delegation
<ul id="list">
<li>Item 1</li>
<li>Item 2</li>
</ul>
<script>
document.getElementById("list").addEventListener("click", (event) => {
// Print `"Clicked:", event.target.textContent` to the console
console.log("Clicked:", event.target.textContent);
});
</script>
Custom Events
The CustomEvent constructor lets you create and dispatch your own named events, complete with custom data attached, allowing different parts of an application to communicate using the same familiar event system browsers use natively.
उदाहरण: Custom Events
// Listen for the `greet` event on `document` and run the handler when it fires
// Listen for the `greet` event on `document` and run the handler when it fires
document.addEventListener("greet", (e) => console.log("Received:", e.detail));
// Declare the constant `event` as a new `CustomEvent` instance
// Declare the constant `event` as a new `CustomEvent` instance
const event = new CustomEvent("greet", { detail: "Hello!" });
// Call `document.dispatchEvent(event)`
// Call `document.dispatchEvent(event)`
document.dispatchEvent(event);
preventDefault()
preventDefault() stops a browser's default built-in action for an event, like following a link's href or submitting a form and reloading the page, letting JavaScript take full control of what happens instead.
उदाहरण: preventDefault()
<a href="https://example.com" id="link">Click</a>
<script>
document.getElementById("link").addEventListener("click", (e) => {
// Call `e.preventDefault()`
e.preventDefault();
// Print "Navigation stopped" to the console
console.log("Navigation stopped");
});
</script>
stopPropagation()
stopPropagation() stops an event from continuing to bubble upward through parent elements after it's been handled, which is useful when a click on a nested element shouldn't also trigger a listener attached to one of its ancestors.
उदाहरण: stopPropagation()
<div id="outer">
<button id="inner">Click</button>
</div>
<script>
// Listen for the `click` event on `document.getElementById("outer")` and run the handler when it fires
document.getElementById("outer").addEventListener("click", () => console.log("Outer"));
document.getElementById("inner").addEventListener("click", (e) => {
// Call `e.stopPropagation()`
e.stopPropagation();
// Print "Inner only" to the console
console.log("Inner only");
});
</script>
A Quick Reference of Event Types
Beyond click, common event types include keydown and keyup for keyboard input, submit for forms, input for real-time text field changes, and scroll for tracking page or element scrolling, each useful in different interactive situations.
उदाहरण: A Quick Reference of Event Types
document.addEventListener("keydown", (e) => console.log("keydown:", e.key));
document.addEventListener("scroll", () => console.log("scroll"));
// Also common: keyup, submit, input
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic