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.
I do not know that there is this type of event, what comes to mind is creating the event "el-load" based on this tutorial, and then extend "append" to know if the item has this event make the call to it.
Try these:
or to be safe this one:
basically, this will loop every 200ms until the selector gets result, and terminates the loop when the result is available
try overwriting the append method so you can add your own event?
You want the livequery plugin, which does just this. The recent
live
function is similar, except it won't call you when the element is added. We use it all the time-- works great.You'll use
$('h1').livequery(function() {alert('just added');});
Use LiveQuery (jQuery plugin), and attach a load event to ur dom element (h1), in this case.
If the tag is being created via ajax, you can use a related node to subscribe to the ajaxSuccess event.
http://docs.jquery.com/Ajax/ajaxSuccess
If it's just being added to the DOM by a local script, I'm not sure it's possible to observe it's creation, with the exception of using a timer to poll for it.