Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to jQuery livequery plugin?

I need to check when an element is alive, I'm doing:

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

How can I do it with live method or another way?

like image 203
Codnickers453 Avatar asked Jul 01 '11 18:07

Codnickers453


2 Answers

If it is your code that is adding the element to the DOM, then run your code on it when you create it:

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

var obj = $('#obj');

obj.runSomeCode();

...or you can even do it before it is appended:

var obj = $('<div id="obj">some new object</div>');

obj.runSomeCode();

obj.appendTo('body');
like image 42
user113716 Avatar answered Oct 21 '22 05:10

user113716


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


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

I use it, for example, in this way:

// 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 the Publish/Subscribe Pattern for Greater JavaScript Scalability

like image 99
Igor Parra Avatar answered Oct 21 '22 03:10

Igor Parra