<script>
require(['jquery'], function($) {
$(document).ready( function() {
var checkExist = setInterval(function() {
if ($('.element__class').length) {
// do something
clearInterval(checkExist);
}
}, 500); // check every 500ms
});
});
</script>
Created
May 22, 2023 10:36
-
-
Save DominicWatts/9841b86d216e1c5182df1c03153afc92 to your computer and use it in GitHub Desktop.
Magento 2 : jquery execute once element exists #magento2
<script>
// Function to load a script dynamically
function loadScript(url, callback) {
const script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.onload = callback;
document.head.append(script);
}
// Load jQuery first
loadScript('https://code.jquery.com/jquery-3.6.0.min.js', function() {
// jQuery is loaded, now load Slick Slider
loadScript('https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.js', function() {
// Slick Slider is loaded, now initialize it
$(document).ready(function() {
var checkExist = setInterval(function() {
if ($('.element__class').length) {
// Initialize Slick Slider (or perform any action)
$('.element__class').slick({
// Slick Slider settings
dots: true,
infinite: true,
speed: 300,
slidesToShow: 1,
adaptiveHeight: true
});
clearInterval(checkExist);
}
}, 500); // Check every 500ms
});
});
});
</script>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.