← Back to jQuery Course | Chapter 15: Performance & Best Practices | Lesson 3 of 5

Efficient Event Handling

Event delegation एक event handler को एक parent पर attach करता है और matching child elements से events handle करता है। dynamic content के लिए यह उपयोगी है।
Syntax
javascript
$(parent).on("eventName", "childSelector", function () {
  // delegated handler
});

Event Delegation क्या है?

Event delegation एक single listener को एक stable parent element पर attach करता है और इसे matching descendants से bubble होकर आने वाले events पकड़ने देता है, हर एक पर bind करने के बजाय।

उदाहरण: What is Event Delegation?

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <ul id="list"><li>Item 1</li></ul>
    <script>
      $("#list").on("click", "li", function() {
      console.log("Clicked:", $(this).text());
      });
    </script>
  </body>
</html>

Dynamic Elements

Delegation उन elements के लिए काम करता है जो event handler originally बनने के बाद page में add किए गए हैं, जो directly handlers bind करने पर एक बड़ा advantage है।

उदाहरण: Dynamic Elements

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <ul id="list"><li>Item 1</li></ul>
    <button id="add">Add Item</button>
    <script>
      $("#list").on("click", "li", function() {
      console.log($(this).text());
      });
      $("#add").on("click", function() {
      $("#list").append("<li>New Item</li>");
      });
    </script>
  </body>
</html>

Direct Binding बनाम Delegation

Direct binding हर मौजूदा element पर individually एक handler attach करता है, जिसका मतलब है बाद में add किए गए किसी भी element को अपना नया handler attach करना होगा।

उदाहरण: Direct Binding vs Delegation

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <ul id="list"><li>Item 1</li></ul>
    <script>
      $("#list li").on("click", function() { console.log("Direct"); });
      $("#list").on("click", "li", function() { console.log("Delegated"); });
    </script>
  </body>
</html>

अलग-अलग Events Delegate करना

on() method इस तरह कई अलग-अलग event types delegate कर सकता है — click, mouseenter, change, और दूसरे — दूसरे argument के रूप में एक selector pass करके।

उदाहरण: Delegating Different Events

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <ul id="list"><li>Item 1</li></ul>
    <script>
      $("#list").on("click", "li", function() { console.log("Clicked"); });
      $("#list").on("mouseenter", "li", function() { console.log("Hovered"); });
    </script>
  </body>
</html>

एक छोटा Delegation Project

Delegation lists, tables, menus, और किसी भी ऐसे interface के लिए खासतौर पर valuable है जो dynamically elements create या remove करता है, क्योंकि यह bookkeeping से बचाता है।

उदाहरण: Small Delegation Project

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <style>
      .selected { background: yellow; }
    </style>
  </head>
  <body>
    <table id="rows"><tr><td>Row 1</td></tr></table>
    <script>
      $("#rows").on("click", "tr", function() {
        $(this).toggleClass("selected");
      });
    </script>
  </body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. $("li").on("click", ...) को हर item पर bind करना और बाद में add किए गए items के लिए काम करने की उम्मीद रखना, जबकि $("#list").on("click", "li", ...) जैसी delegation चाहिए।
  2. हर चीज़ के लिए $(document) पर delegate करना, जो page पर हर click को handler से गुज़रने पर मजबूर करता है; सबसे नज़दीकी stable parent इस्तेमाल करें।
  3. deprecated .live() या .delegate() इस्तेमाल करना, जबकि एक selector के साथ .on() दोनों की जगह ले लेता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.