Search code examples
javascriptjquerylightgallerylightgallery-1

TypeError: $(...).lightGallery(...).destroy is not a function


I am getting this error from firefox:

TypeError: $(...).lightGallery(...).destroy is not a function

I am using following plugin for play video and show image gallery https://github.com/sachinchoolur/lightGallery.

I want re-initialize lightGallery because I am using ajax to add element dynamically in container. It was working fine with previous version of lightGallery but it is not working with current version.

I am using following code.

// destroy previous gallery
$("#lightGallery2").lightGallery({
  selector : '.image_gallery',
  videojs: true,
  download: false
}).destroy();

// re-initialize
$("#lightGallery2").lightGallery({
  selector: '.image_gallery',
  videojs: true,
  download: false
});

Please suggest.

Thanks


Solution

  • The following code works for me:

    $lg.on('onBeforeClose.lg',function(event, index, fromTouch, fromThumb){
        try{$lg.data('lightGallery').destroy(true);}catch(ex){};
    });