0

I know in jQuery that there's the function live() to suscribe to events as soon as an element appears. I just want to do something to an element as soon as it appears. I tried suscribing the elements to the "load" event but it doesn't work. How do you go about that?

Fernando Tiberti
  • 1,272
  • 2
  • 10
  • 18

4 Answers4

1

you can't use load to all elements . only : Img Body Iframe.

and you can't listen via live to the load event of elements other the ones i mentioned.

what you can do - is when you add them to the Page - Then you know when its happened and then you can call your func.

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
1

If you control the code which creates and appends elements, you could trigger a custom event and handle it in a handler later.

Example

$('#add').click(function() {
    $('#container').append($('<div>Hello! I am a new div</div>')).trigger('div-added');
});

$('#container').bind('div-added', function() {
    alert('A div has been added');
});
Sahil Muthoo
  • 12,033
  • 2
  • 29
  • 38
0

Use .delegate() http://api.jquery.com/delegate instead of .live()

For your specific case consider livequery http://docs.jquery.com/Plugins/livequery

Moin Zaman
  • 25,281
  • 6
  • 70
  • 74
0

You can listen to DOMNodeInserted. I know that works in Webkit and Firefox - I don't know about IE though.

It would look something like this:

$('body').bind('DOMNodeInserted', function(evt) {
    var addedElement = evt.target;
    // do whatever with `addedElement`...
})
Lee
  • 13,462
  • 1
  • 32
  • 45
  • `DOMNodeInserted` and other DOM mutation events have been deprecated in DOM 3. See http://www.w3.org/TR/DOM-Level-3-Events/#events-mutationevents. – Sahil Muthoo Oct 23 '11 at 06:00