JQuery: .live () and trigger () do not work together

$('#id').bind('change',function() { //do something }).trigger('change'); 

It works great. but if id2 was generated using AJAX

I'm trying to use

 $('#id2').live('change',function() { //do something }).trigger('change'); 

But it does not work. Can someone help me. Thanks

+4
source share
2 answers

If you write

 $('#id2').live('change',function() { //do something }); 

then you do it because #id2 has not been created yet. trigger('change') , on the other hand, immediately triggers an event. But if the element does not exist yet, calling it has no effect.

You need to call trigger() after creating the element:

 $('#id2').trigger('change'); 

There is no need to use live() if #id2 already exists. You can just use bind() .

+10
source

Have you tried to raise a change event in the ajax-complete function?

0
source

All Articles