What is event delegation in jQuery and why is it useful?
<ul id="list"><li>A</li></ul>
$('#list').on('click', 'li', function () { console.log('clicked ' + $(this).text()); });
$('#list').append('<li>B</li>'); // added later
$('#list li').eq(1).click();
Console output
clicked B
It saves memory (one handler) and works for dynamic content. See Event Delegation.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |