jQuery attach function to 'load' event of an element

Posted by Miguel Ping on Stack Overflow See other posts from Stack Overflow or by Miguel Ping
Published on 2009-11-17T10:58:10Z Indexed on 2010/04/16 9:33 UTC
Read the original article Hit count: 291

Hi,

I want to attach a function to a jQuery element that fires whenever the element is added to the page.

I've tried the following, but it didn't work:

var el = jQuery('<h1>HI HI HI</H1>');   
el.one('load', function(e) {
  window.alert('loaded');
});    
jQuery('body').append(el);

What I really want to do is to guarantee that another jQuery function that is expecting some #id to be at the page don't fail, so I want to call that function whenever my element is loaded in the page.


To clarify, I am passing the el element to another library (in this case it's a movie player but it could be anything else) and I want to know when the el element is being added to the page, whether its my movie player code that it is adding the element or anyting else.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript