Skip to main content

Swiper instance not yet available on window.load

I need to setup the click event on an Elementor carousel widget, which is swiper.js 8 under the hood. Problem is, depending on the page (same code works elsewhere), not even when window.load executes the swiper instance is available to work with:

jQuery(window).on('load', function(){
    const swiperTarget = jQuery('#dp-neighborhoods .swiper');
    const swiperNeigh = swiperTarget.data('swiper');
    if (swiperNeigh) {
        alert('found it'); //it doesn't
        swiperNeigh.on('click', clickSwiper);
    }
    
    function clickSwiper(swiper, event) {...}

I can only get this to work if I call the function from a button, which is not ideal. A delay (Promise(resolve => setTimeout(resolve, time)) doesn't help either. Would there be another way to make this work?

Via Active questions tagged javascript - Stack Overflow https://ift.tt/eM5WYGC

Comments