How to bind change () function to dynamically created <select>?

I am creating elements in the DOM by JS. The question is, how do I assign a .change () function to each newly created item?

+3
source share
4 answers

Even better than that .live(), in terms of performance, .delegate(). This works the same way .live(): it uses something called a bubble event, which means that when an event is triggered in a DOM element, all parents of that element are notified of the event. delegatehowever, it has slightly better syntax and a significant performance advantage.

$('#parentElement').delegate('select.yourSelectClass', 'change', function() {
    // do your processing here
});

#parentElement , select, . document.body, , , DOM, , , .

select.yourSelectClass , yourSelectClass. , jQuery, .

+6

jQuery.live() :

$('.yourSelectClass').live('change', function(event){
    // Handle the Change event here.
});
+4

jQuery live. ( 1.3 )

$(".classOfJSCreatedElements").live('change',
function(event){
//do stuff
});

jQuery, livequery

: jQuery FAQ

+1

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


All Articles