Click () assigned in document.ready in jQuery

Will the document.ready document (click (fn)) give way to newly added elements that match the selector?

If not, how can I assign it to these new elements? Should I write a task after each addition, or is there a better way?

+3
source share
1 answer

You are looking for live . In the manual:

Binds a handler to an event (for example, a click) for all elements corresponding to the current and future. May also bind custom events.

So if you do this:

$(document).ready(function() {
    $('div.test').live('click', function() { alert('yipee!'); });
    $('body').append('<div class="test">Click me!</div>');
});

When you click on a div, you will get a warning even if it was added after the event was connected.

+7

Source: https://habr.com/ru/post/1705949/


All Articles