Alternative to jQuery livequery plugin?
Asked Answered
L

4

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?

Lade answered 1/7, 2011 at 18:19 Comment(3)
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(...);?)Epicureanism
i'll generate this '#obj' with javascript then it's dynamicallyLade
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.Lade
A
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

Ashraf answered 30/10, 2012 at 16:57 Comment(0)
C
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');
Connoisseur answered 1/7, 2011 at 18:22 Comment(3)
Well, as an alternative to using livequery, you could run the function yourself when you fade the element into the page.Epicureanism
@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.Connoisseur
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.Lectra
M
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);
Maiamaiah answered 15/1, 2014 at 9:12 Comment(0)
M
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+

Maternal answered 22/12, 2016 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.