Created
March 15, 2021 22:48
-
-
Save johnalarcon/2938e8ec25cd347ae784889b6d433988 to your computer and use it in GitHub Desktop.
Await existence of element before executing jQuery on it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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