1

Possible Duplicate:
jQuery: how to replace .live with .on?

.live() was deprecated in jQuery 1.7 and removed in version 1.9. I just upgraded jQuery to 1.9.0 in our application and I saw that we used tons of .live() calls in the past.

I ported all the .live() calls to .on() calls. In most cases it's a straightforward update (simple cases like the one in the second snippet on the .live() page). But at several places I have something like:

(function($){
    $(document).ready(function() {
        $('.ui-jqgrid-btable').find('a.pop_dialog_jqgrid').live('click', function() {
            //some code here
        });
    });
})(jQuery);

which I have ported to:

(function($){
    $(document).ready(function() {
        $(document).on('click', $('.ui-jqgrid-btable').find('a.pop_dialog_jqgrid'), function() {
            //some code here
        });
    });
})(jQuery);

But it's not doing the same thing at all with the new code (all the clicks anywhere on the page fire the "some code").

Anyone can enlighten me please?

Community
  • 1
  • 1
AlexV
  • 21,903
  • 16
  • 86
  • 119

2 Answers2

3

The second argument should be a selector, not an object:

$(document).on('click', '.ui-jqgrid-btable a.pop_dialog_jqgrid', function() {
     //some code here
});
Joseph Silber
  • 205,539
  • 55
  • 352
  • 286
1

You need a selector, not a jQuery object. Personally, I would localize it a bit more than document.

$('.ui-jqgrid-btable').on('click', 'a.pop_dialog_jqgrid', function() {
    //some code here
});
PlantTheIdea
  • 15,781
  • 5
  • 32
  • 40