Alternative to jQuery livequery plugin?

I need to check when an element is alive, I do:

$('#obj').livequery(function() { ... }); 

How can I do this using a live method or in another way?

+7
source share
4 answers

This is an old question, but for the record here, my 5 cents:


An alternative to livequery is to use the Publish/Subscribe approach, for example, this jQuery Tiny Pub / Sub implementation : really, really, REALLY tiny pub / sub implementation for jQuery.

I use it, for example, as follows:

 // inform of DOM change (mostly through AJAX) $.publish("/table/loaded"); // re-execute plugin $.subscribe("/table/loaded", function(e, data) { $("#my_id input[name='date']").datepicker(); }); 

+ info: Understanding publish / subscribe template for greater scalability of JavaScript

+2
source

If this is your code that adds an element to the DOM, then run its code when creating it:

 $('body').append('<div id="obj">some new object</div>'); var obj = $('#obj'); obj.runSomeCode(); 

... or you can even do this before adding it:

 var obj = $('<div id="obj">some new object</div>'); obj.runSomeCode(); obj.appendTo('body'); 
+1
source

As an alternative to the livequery plugin, you can watch the $ .whenLive jQuery plugin:

$. whenLive allows you to track the insertion of the DOM tree of one or more elements.

http://bitcubby.com/tracking-the-insertion-of-javascript-components-into-the-dom/

Here is an example from this page:

 var widget = $("<div>I am a nobody. Nobody is perfect. Therefore, I am perfect.</div>"); $(widget).onLive(function() { // Awesomesauce. var height = $(this).height(); var width = $(this).width(); }); $("body").append(widget); 
+1
source

This is an old question, but I am surprised that there is no answer, because it is simple. You must use on () and attach the event to the parent or body. Example:

 $('#obj').livequery('click', function() { ... }); $('#obj').livequery(function() { ... }); 

to become

 $('body').on('click', '#obj', function() { ... }); $('body').on('DOMNodeInserted','#obj', function() { ... }); 

note that DOMNodeInserted is IE9 +

0
source

All Articles