Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery attach function to 'load' event of an element

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.

like image 202
Miguel Ping Avatar asked Nov 17 '09 10:11

Miguel Ping


People also ask

Which jQuery method is used to attach an event listener to an element?

bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs.

Which jQuery method triggers or binds a function to the error event of selected elements?

jQuery | error() Method The error() method in jQuery is used when an element encounters an error that is the element is not loaded correctly. The error() method attaches a function to run when an error event occurs or it triggers the error event.

What elements can you attach jQuery events to?

jQuery's event system requires that a DOM element allow attaching data via a property on the element, so that events can be tracked and delivered. The object , embed , and applet elements cannot attach data, and therefore cannot have jQuery events bound to them.

How do you call one event from another event in jQuery?

$('#b1'). on('click', function(){ $('#select_item'). trigger('change'); }); $('#select_item'). on('change', function(){ var jbyr = $(this).


2 Answers

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

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');});

like image 111
ndp Avatar answered Sep 23 '22 05:09

ndp


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.

like image 23
andres descalzo Avatar answered Sep 24 '22 05:09

andres descalzo