Skip to content Skip to sidebar Skip to footer

Running A Callback Once An Element Is Created

Problem: I am creating an element via a JS templating system. Inside that template I am specifying an ID. After that ID is created, Is there a way with jQuery to fire a callback wh

Solution 1:

You could run an interval to check for the id in the dom, and then fire an event if it exists.

var intv = setInterval(function(){
 var $el = $("#myId");

 if ( $el.length > 0 ) {
   clearInterval(intv);
   doSomething();
 }
}, 500);

probably should clear the interval after 10 secs in the event if never shows:

setTimeout(function(){
  clearInterval(intv);
}, 10000);

Post a Comment for "Running A Callback Once An Element Is Created"