jQuery - how to use the "on()" method instead of "live()"? [duplicate]
Asked Answered
T

1

26

I used live() for generated pages and frames. But in jQuery 1.9 this function is deprecated and does not work.

I use on() instead of live() but this method works for one time, and does not work in frames.

My code looks like this:

  $("#element").live('click',function(){
    $("#my").html(result);
   });

What is the solution?

Tuantuareg answered 5/2, 2013 at 9:6 Comment(2)
Read the docs. Delegate the event handler higher up the DOM tree.Biagi
The documentation for .live() also has a section specifically about converting from .live() to .on().Nimwegen
S
73
$('body').on('click', '#element', function(){
    $("#my").html(result);
});

The clicked element selector is now passed through the .on() function parameters, and the previous selector should be replaced with the closest parent selector preferably with an ID. If you do not know what parent selector to use, body works too, but is less efficient.

see jQuery 1.9 .live() is not a function on how to migrate existing code.

Solnit answered 5/2, 2013 at 9:7 Comment(1)
thanx... nice answer...Holily

© 2022 - 2024 — McMap. All rights reserved.