-1

I have been using .live() in my jquery code but it some how fails when the html is generated dynamically, so is there a alternative for .live() in jquery?

PUBG
  • 199
  • 2
  • 12

3 Answers3

1

Firstly, .live is depreciated removed, and is replaced with .on:

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live()

If you're generating elements "on the fly", you need to bind them to a function, and as they're not already in the DOM, you'll need to bind to the document, and delegate to the specific element:

$(document).on("event", "element", function(){
    //do what you need here
});

As you've not provided any code, I can only give you a general idea as demonstrated above

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
0

.live() is depreciated, use .on() instead.

Florian Gl
  • 5,984
  • 2
  • 17
  • 30
-3

the purpose of live is exactly to works when html is generated dynamically. Maybe you have to put your .live call inside a $(document).ready :

$(document).ready(function(){
  $("class selector").live(function(e){
    // your code here
  })
});

and, like commented by others in the question, live() should be replaced by a new syntax you can see here : Turning live() into on() in jQuery

Community
  • 1
  • 1
Asenar
  • 6,732
  • 3
  • 36
  • 49
  • It doesn't make much sense to wrap `.live()` in a document ready event since its purpose is, as you said, to work regardless of when the target elements are created. – JJJ Nov 04 '13 at 10:27
  • Yes it makes a lot of sense : without document.ready, `$` may not be loaded. – Asenar Nov 04 '13 at 10:29
  • 2
    ... how are you using `$(document).ready()` if `$` isn't available? – JJJ Nov 04 '13 at 10:31
  • That's a good question I can't answer to. I know that by experience: sometimes on really slow server, $("").live() gives an error "$ is not defined", and it never does when inside a .ready – Asenar Nov 04 '13 at 10:39