Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
jQuery plugin which runs handler function once specified element is inserted into the DOM
(function ($) {
/**
* @function
* @property {object} jQuery plugin which runs handler function once specified element is inserted into the DOM
* @param {function} handler A function to execute at the time when the element is inserted
* @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation
* @example $(selector).waitUntilExists(function);
*/
$.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild) {
var found = 'found';
var $this = $(this.selector);
var $elements = $this.not(function () { return $(this).data(found); }).each(handler).data(found, true);
if (!isChild)
{
(window.waitUntilExists_Intervals = window.waitUntilExists_Intervals || {})[this.selector] =
window.setInterval(function () { $this.waitUntilExists(handler, shouldRunHandlerOnce, true); }, 500)
;
}
else if (shouldRunHandlerOnce && $elements.length)
{
window.clearInterval(window.waitUntilExists_Intervals[this.selector]);
}
return $this;
}
}(jQuery));
@mstonerock

This comment has been minimized.

Show comment Hide comment
@mstonerock

mstonerock Dec 6, 2012

Thank you, Thank you, Thank you. I'm a total JS/Jquery noob and this issue was driving me crazy. I've got a project where I'm dynamically creating a element and then trying to use .change() to do things when are selected. The builds just fine and the .change() code looked good but it wouldnt work. Injected your waitUntilExists function and it works like a charm. Now I can stop beating my head on the keyboard wondering what's not working.

Thank you, Thank you, Thank you. I'm a total JS/Jquery noob and this issue was driving me crazy. I've got a project where I'm dynamically creating a element and then trying to use .change() to do things when are selected. The builds just fine and the .change() code looked good but it wouldnt work. Injected your waitUntilExists function and it works like a charm. Now I can stop beating my head on the keyboard wondering what's not working.

@uwanius

This comment has been minimized.

Show comment Hide comment
@uwanius

uwanius Apr 25, 2013

Hi, thank you for this great plugin. But how it is possible to run the function not only once, but every time.
I used it in the flexslider plugin to find out when a slider is active and then add a special class to the navigation.

my code:

var secondSlide =$("#secondary-slider li:eq(1).flex-active-slide");
var next2 = $('#home .flex-direction-nav li:eq(1) a');

$(secondSlide).waitUntilExists(function() {
next2.addClass('flex-disabled')
});

This works fine, but only at once.

uwanius commented Apr 25, 2013

Hi, thank you for this great plugin. But how it is possible to run the function not only once, but every time.
I used it in the flexslider plugin to find out when a slider is active and then add a special class to the navigation.

my code:

var secondSlide =$("#secondary-slider li:eq(1).flex-active-slide");
var next2 = $('#home .flex-direction-nav li:eq(1) a');

$(secondSlide).waitUntilExists(function() {
next2.addClass('flex-disabled')
});

This works fine, but only at once.

@PizzaBrandon

This comment has been minimized.

Show comment Hide comment
@PizzaBrandon

PizzaBrandon Jun 5, 2013

I forked and improved on your code a bit, if you want to pull in some or all of my changes. Thanks!

I forked and improved on your code a bit, if you want to pull in some or all of my changes. Thanks!

@alexbeletsky

This comment has been minimized.

Show comment Hide comment
@alexbeletsky

alexbeletsky May 6, 2014

Thanks a lot!

Thanks a lot!

@beames

This comment has been minimized.

Show comment Hide comment
@beames

beames Oct 11, 2014

Thanks!!!! Immediately solved the problem

beames commented Oct 11, 2014

Thanks!!!! Immediately solved the problem

@dsanatelli

This comment has been minimized.

Show comment Hide comment
@dsanatelli

dsanatelli Oct 21, 2014

Thanks !!

Thanks !!

@kdamken

This comment has been minimized.

Show comment Hide comment
@kdamken

kdamken Mar 10, 2015

Thank you so much, this solved a very large headache for me!

kdamken commented Mar 10, 2015

Thank you so much, this solved a very large headache for me!

@rorycaraher

This comment has been minimized.

Show comment Hide comment
@rorycaraher

rorycaraher Apr 23, 2015

Brilliant and easy! Thank you for this.

Brilliant and easy! Thank you for this.

@code2be

This comment has been minimized.

Show comment Hide comment
@code2be

code2be May 10, 2015

Thank You for this great piece of code !

code2be commented May 10, 2015

Thank You for this great piece of code !

@rpoddighe

This comment has been minimized.

Show comment Hide comment
@rpoddighe

rpoddighe May 21, 2015

I love you. Not a pretty fix in my case but it does the trick.

I love you. Not a pretty fix in my case but it does the trick.

@diegommarino

This comment has been minimized.

Show comment Hide comment
@diegommarino

diegommarino Jun 18, 2015

Thank you so much! That's perfect!

Thank you so much! That's perfect!

@360disrupt

This comment has been minimized.

Show comment Hide comment
@360disrupt

360disrupt Sep 21, 2015

Exactly what I'm looking for.

Exactly what I'm looking for.

@tonyporto

This comment has been minimized.

Show comment Hide comment
@tonyporto

tonyporto Oct 7, 2015

Just had to stop by and say Thanks man, this is the only plugin that worked for me, nice clean, simple.. Good work

Just had to stop by and say Thanks man, this is the only plugin that worked for me, nice clean, simple.. Good work

@ChristianLuxem

This comment has been minimized.

Show comment Hide comment
@ChristianLuxem

ChristianLuxem Aug 10, 2016

This is not working anymore in Jquery3, because the .selector function was removed.

This is not working anymore in Jquery3, because the .selector function was removed.

@ChaosPredictor

This comment has been minimized.

Show comment Hide comment
@ChaosPredictor

ChaosPredictor Oct 17, 2016

Any suggestion for Rails 5?

Any suggestion for Rails 5?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment