JS एडवांस्ड Events
In this page:
element.addEventListener("eventName", handler, options);
element.removeEventListener("eventName", handler);
Event Delegation
Event delegation event bubbling पर निर्भर करता है, किसी parent element पर एक single listener attach करना और event object की target property का इस्तेमाल करके यह पता लगाना कि असल में किस child element ने उसे trigger किया, हर child पर अलग-अलग listener attach करने के बजाय।
उदाहरण: 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
CustomEvent constructor आपको अपने खुद के named events बनाने और dispatch करने देता है, custom data के साथ, जो application के अलग-अलग हिस्सों को browsers के अपने native event system से ही बातचीत करने देता है।
उदाहरण: 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() किसी event के लिए browser की default built-in action को रोक देता है, जैसे किसी link के href को follow करना या form submit करके page reload करना, जिससे JavaScript इसके बजाय पूरा control ले लेता है कि क्या होता है।
उदाहरण: 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() किसी event को handle होने के बाद parent elements तक ऊपर bubble होते रहने से रोकता है, जो तब उपयोगी है जब किसी nested element पर click को उसके किसी ancestor पर attached listener को भी trigger नहीं करना चाहिए।
उदाहरण: 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>
Event Types की एक Quick Reference
click के अलावा, आम event types में keyboard input के लिए keydown और keyup, forms के लिए submit, real-time text field changes के लिए input, और page या element scrolling track करने के लिए scroll शामिल हैं, हर एक अलग-अलग interactive स्थितियों में उपयोगी।
उदाहरण: 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
- delegation इस्तेमाल करने के बजाय कई dynamically created elements पर individual event listeners attach करना, जो memory बर्बाद करता है और future elements को miss कर देता है।
- form submission पर preventDefault() भूल जाना जब आप specifically उसे पूरी तरह JavaScript से handle करना चाहते हैं, असली page reload के बजाय।
- यह समझे बिना stopPropagation() कॉल करना कि यह DOM tree में ऊपर मौजूद दूसरे legitimate listeners को भी कभी न चलने से रोक देता है।
- Event delegation एक parent element पर एक listener attach करता है, यह पहचानने के लिए event.target इस्तेमाल करते हुए कि असल में कौन सा specific child interact हुआ।
- CustomEvent constructor से बने Custom events आपके code के अलग-अलग हिस्सों को native browser events जैसे same event system से communicate करने देते हैं।
- preventDefault() किसी browser की default action रोकता है, और stopPropagation() किसी event को parent elements तक bubble होते रहने से रोकता है।
Event delegation, custom events, preventDefault, और stopPropagation सभी standard DOM features हैं जो हर modern browser में समान रूप से supported हैं।
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