Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: .live() and trigger() not working together

Tags:

jquery

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

it works fine. but if id2 has been generated by using AJAX

i am trying to use

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

But its not working. Can anybody help me please. Thanks

like image 801
bee Avatar asked Dec 29 '22 04:12

bee


1 Answers

If you write

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

then you do this because #id2 is not 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() once the element is created:

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

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

like image 194
Felix Kling Avatar answered Dec 30 '22 17:12

Felix Kling