7

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?

Lundin
  • 195,001
  • 40
  • 254
  • 396
Codnickers453
  • 95
  • 1
  • 8
  • Is it something you're generating dynamically (more than once?) or is it present on your page when it's loaded? (i.e. can you just use `$(document).ready(...);`?) – Rup Jul 01 '11 at 18:20
  • i'll generate this '#obj' with javascript then it's dynamically – Codnickers453 Jul 01 '11 at 18:21
  • idk why but when i do fadeIn() in some element, the livequery plugin won't work cuz it i want to find a new alternative. – Codnickers453 Jul 01 '11 at 18:35

4 Answers4

2

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

Igor Parra
  • 10,214
  • 10
  • 69
  • 101
1

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);
solognt
  • 56
  • 5
1

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');
user113716
  • 318,772
  • 63
  • 451
  • 440
  • Well, as an alternative to using livequery, you could run the function yourself when you fade the element into the page. – Rup Jul 01 '11 at 18:53
  • 1
    @Rapososoo: This is an alternative to using livequery. If you don't want livequery to do it automatically, you need to code it manually, which is a better practice anyway IMO. – user113716 Jul 01 '11 at 21:23
  • 2
    The problem is there are multiple clicks that create the same elements. Livequery is great because you focus on was created instead of who created it. – TruMan1 Jul 14 '12 at 11:23
0

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+

Reign.85
  • 2,420
  • 1
  • 28
  • 28