Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind ready event on javascript dom element creation

i would like to know if we can bind a loaded or ready event on an item created by a script when the dom is loaded. I heard from live() but it's not something clickable, it's just an item which has to load.

Thanks for your help!

like image 815
Artusamak Avatar asked Aug 16 '10 14:08

Artusamak


People also ask

Which method is used to bind a callback method for an event for DOM elements added at runtime?

You can use the live() method to bind elements (even newly created ones) to events and handlers, like the onclick event.

How do you bind an event in JavaScript?

jQuery bind() MethodUse the on() method instead. The bind() method attaches one or more event handlers for selected elements, and specifies a function to run when the event occurs.

How do you grab elements from DOM?

The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID with the getElementById() method of the document object. In the Console, get the element and assign it to the demoId variable. const demoId = document.


1 Answers

I guess your best shot is the load event there.

$('element').load(function(){
   alert('loaded');
});

native

var elem = document.getElementById('element_id');
elem.onload = function(){
   alert('loaded');
};

Another example for dynamic creation:

$('<img/>', {
   src:   '/images/myimage.png',
   load:  function(){
     alert('image loaded');
   }
}).appendTo(document.body);
like image 79
jAndy Avatar answered Sep 26 '22 01:09

jAndy