Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save DominicWatts/9841b86d216e1c5182df1c03153afc92 to your computer and use it in GitHub Desktop.

Select an option

Save DominicWatts/9841b86d216e1c5182df1c03153afc92 to your computer and use it in GitHub Desktop.
Magento 2 : jquery execute once element exists #magento2
<script>
require(['jquery'], function($) {
    $(document).ready( function() {
        var checkExist = setInterval(function() {
            if ($('.element__class').length) {
                // do something
                clearInterval(checkExist);
            }
        }, 500); // check every 500ms
    });
});
</script>
@DomPixie

DomPixie commented Jul 18, 2024

Copy link
Copy Markdown
<script>
    // Create a script element
    const script = document.createElement('script');
    // Set the source to the jQuery library
    script.src = 'https://code.jquery.com/jquery-3.6.0.min.js';
    script.type = 'text/javascript';
    // Append the script to the document head
    document.head.append(script);

    // Use the 'load' event to ensure jQuery is loaded before using it
    script.onload = function() {
        $(document).ready(function() {
            var checkExist = setInterval(function() {
                if ($('.element__class').length) {
                    // Do something when the element is found
                    clearInterval(checkExist);
                }
            }, 500); // Check every 500ms
        });
    };
</script>

@DomPixie

Copy link
Copy Markdown
<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