Skip to content

Instantly share code, notes, and snippets.

@johnalarcon
Created March 15, 2021 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnalarcon/2938e8ec25cd347ae784889b6d433988 to your computer and use it in GitHub Desktop.
Save johnalarcon/2938e8ec25cd347ae784889b6d433988 to your computer and use it in GitHub Desktop.
Await existence of element before executing jQuery on it
// This can go into whatever file is enqueued for the given page, using the traditional admin_enqueue_scripts hook.
jQuery(document).ready(function($) {
var onElementExists = function(element, callback) {
if ($(element).length) {
callback($(element));
}
function check_again(element, callback) {
setTimeout(function() {
onElementExists(element, callback);
}, 100); // interval
}
check_again(element, callback);
};
// Do stuff when element exists.
onElementExists('.mce-floatpanel', function(e) {
$('.mce-floatpanel').css('background', '#f00'); // PoC: change modal background with jQuery
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment