1

I work with cordova and jquery, and i try to use Swiper by idangero.

I tried to do this :

$(document).ready(function () { //initialize swiper when document ready var mySwiper = new Swiper ('.swiper-container', { // Optional parameters autoplay: ‘3000’, loop: true }) }); 

but when i call it in this part:

$(document).on('pagebeforeshow', ‘#listDetail’, function(){ … some codes … if(row.pictures != ''){ var numslide = mySwiper.slides.length; mySwiper.appendSlide(slidecontent); for (var i = 0; i < numslide; i++) { mySwiper.removeSlide(0); } mySwiper.updateSlidesSize(); }else{ mySwiper.removeAllSlides(); } … some codes … }); 

i have this Uncaught ReferenceError: mySwiper is not defined message.

Any idea?

Thank you

1 Answer 1

3

You need to define mySwiper before initializing it so it can be used outside of the $(document).ready function scope

var mySwiper; $(document).ready(function () { //initialize swiper when document ready mySwiper = new Swiper ('.swiper-container', { // Optional parameters autoplay: ‘3000’, loop: true }) }); 

Alternatively, you could get the Swiper instance in your other event like this:

$(document).on('pagebeforeshow', ‘#listDetail’, function(){ … some codes … if(row.pictures != ''){ var mySwiper = $('.swiper-container')[0].swiper; var numslide = mySwiper.slides.length; mySwiper.appendSlide(slidecontent); for (var i = 0; i < numslide; i++) { mySwiper.removeSlide(0); } mySwiper.updateSlidesSize(); }else{ mySwiper.removeAllSlides(); } … some codes … }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.