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?
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?
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
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');
As an alternative to the livequery plug-in you might look at the $.whenLive jQuery plugin:
$.whenLive allows you to track the DOM tree insertion of one or more elements.
http://bitcubby.com/tracking-the-insertion-of-javascript-components-into-the-dom/
Here is an example from that 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);
It's an old question but I'm astonished there is no answer as it's simple. You have to use on() and attach the event to a parent or body. Ex :
$('#obj').livequery('click', function() { ... });
$('#obj').livequery(function() { ... });
become
$('body').on('click', '#obj', function() { ... });
$('body').on('DOMNodeInserted','#obj', function() { ... });
note that DOMNodeInserted is IE9+
© 2022 - 2024 — McMap. All rights reserved.
$(document).ready(...);
?) – Epicureanism