Event delegation in jQuery is the process of attaching event handlers to a parent element rather than to each individual child element. This allows you to handle events on dynamically created elements that did not exist when the page was first loaded.
To implement event delegation in jQuery, you can use the on() method to attach the event handler to the parent element and then specify the child element selector as the second argument.
Here is an example of how to use event delegation in jQuery:
In this example, we are using event delegation to handle click events on
li elements within a ul element. The on() method is used to attach the event handler to the
ul element, and the li selector is specified as the second argument.
When the user clicks on an li element, the callback function is called and toggles the
selected class on the clicked element.
Event delegation is important because it allows you to handle events on dynamically created elements, which can be more efficient than attaching event handlers to each individual element. For example, if you have a long list of items that are being added dynamically, it would be impractical to attach an event handler to each item.
By using event delegation, you can attach the event handler to the parent element and handle events on all child elements with a single event handler. This can greatly improve performance and reduce the amount of code you need to write.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Event delegation in jQuery is the process of attaching event handlers to a parent element rather than to each individual child element. This allows you to handle events on dynamically created elements that did not exist when the page was first loaded.
To implement event delegation in jQuery, you can use the on() method to attach the event handler to the parent element and then specify the child element selector as the second argument.
Here is an example of how to use event delegation in jQuery:
In this example, we are using event delegation to handle click events on li elements within a ul element. The on() method is used to attach the event handler to the ul element, and the li selector is specified as the second argument.
When the user clicks on an li element, the callback function is called and toggles the selected class on the clicked element.
Event delegation is important because it allows you to handle events on dynamically created elements, which can be more efficient than attaching event handlers to each individual element. For example, if you have a long list of items that are being added dynamically, it would be impractical to attach an event handler to each item.
By using event delegation, you can attach the event handler to the parent element and handle events on all child elements with a single event handler. This can greatly improve performance and reduce the amount of code you need to write.