The Slider.prepend() function in lightSlider is not functioning as intended for the 'onSliderLoad' event in lightGallery

I implemented lightslider with lightGallery in the onSliderLoad function. There are two sets of code snippets included in my HTML code. You can find the complete code on this jsfiddle link.

Code 1: Displays lightGallery view on page load, rendering images directly from HTML.
Code 2: Shows a button that, when clicked, should display the lightGallery view. It uses 'prepend' to add images dynamically.

Issue: The lightGallery works fine in Code 1 but is not working in Code 2.

$(document).ready(function() {
  $('.demo').hide();
function BuildImageSlider() { 
  // Function to create and show lightSlider
  $('.demo').show();
  var slider = $('#lightSlider').lightSlider({
                gallery:false,
                item:1,
                loop:true,
                thumbItem:9,
                slideMargin:0,
                enableDrag: false,
                currentPagerPosition:'left',
                onSliderLoad: function(el) {
                    el.lightGallery({
                        selector: '#lightSlider .lslide'
                    });
                }   
            });  
            
            var newEl = '<li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-1.jpg" data-src="http://sachinchoolur.github.io/lightslider/img/cS-1.jpg">            <img src="http://sachinchoolur.github.io/lightslider/img/cS-1.jpg" />        </li><li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-2.jpg" data-src="http://sachinchoolur.github.io/lightslider/img/cS-1.jpg">            <img src="http://sachinchoolur.github.io/lightslider/img/cS-2.jpg" />';
            
         slider.prepend(newEl);
         slider.refresh(); 
           } 
  
  
  $('#lightSlider-pageload').lightSlider({
                gallery:true,
                item:1,
                loop:true,
                thumbItem:9,
                slideMargin:0,
                enableDrag: false,
                currentPagerPosition:'left',
                onSliderLoad: function(el) {
                    el.lightGallery({
                        selector: '#lightSlider-pageload .lslide'
                    });
                }   
            });  
            
      
  $( "#refresh" ).click(function() {   BuildImageSlider(); });
  
  });
.lSAction > a {
    z-index: 99 !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lightslider/1.1.3/js/lightslider.min.js"></script>
<script src="http://sachinchoolur.github.io/lightGallery/lightgallery/js/lightgallery.js"></script>
<link href="http://sachinchoolur.github.io/lightGallery/lightgallery/css/lightgallery.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/lightslider/1.1.3/css/lightslider.min.css" rel="stylesheet"/>


<input type="button" id="refresh" value="ButtonClick_LightGallery"/>

<div class="demo">
    <ul id="lightSlider">
    </ul>
</div>

<div class="demo-pageload">
    <ul id="lightSlider-pageload">
      <li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-2.jpg" data-src="http://sachinchoolur.github.io/lightslider/img/cS-1.jpg">            <img src="http://sachinchoolur.github.io/lightslider/img/cS-2.jpg" /><li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-4.jpg" data-src="http://sachinchoolur.github.io/lightslider/img/cS-4.jpg">            <img src="http://sachinchoolur.github.io/lightslider/img/cS-4.jpg" /></li><li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-5.jpg" data-src="http://sachinchoolur.github.io/lightslider/img/cS-5.jpg">            <img src="http://sachinchoolur.github.io/lightslider/img/cS-5.jpg" />        </li>
</ul>
</div>

Answer №1

Issue resolved. Please check the solution below:

$(document).ready(function() {
  $('.demo').hide();
function BuildImageSlider() { 
      $('#lightSlider').empty();
    $(".lSPager").remove();
    $(".lSAction").empty();
  $('.demo').show();
  var sld=$('#lightSlider');
    var newEl = '<li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-1.jpg" data-src="http://sachinchoolur.github.io/lightslider/img/cS-1.jpg">            <img src="http://sachinchoolur.github.io/lightslider/img/cS-1.jpg" />        </li><li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-2.jpg" data-src="http://sachinchoolur.github.io/lightslider/img/cS-1.jpg">            <img src="http://sachinchoolur.github.io/lightslider/img/cS-2.jpg" />';
            
         sld.prepend(newEl);
         
  var slider = $('#lightSlider').lightSlider({
                gallery:false,
                item:1,
                loop:true,
                thumbItem:9,
                slideMargin:0,
                enableDrag: false,
                currentPagerPosition:'left',
                onSliderLoad: function(el) {
                    el.lightGallery({
                        selector: '#lightSlider .lslide'
                    });
                }   
            });  
            
          
           } 
      
  $( "#refresh" ).click(function() {   BuildImageSlider(); });
  
  });
.lSAction > a {
    z-index: 99 !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lightslider/1.1.3/js/lightslider.min.js"></script>
<script src="http://sachinchoolur.github.io/lightGallery/lightgallery/js/lightgallery.js"></script>
<link href="http://sachinchoolur.github.io/lightGallery/lightgallery/css/lightgallery.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/lightslider/1.1.3/css/lightslider.min.css" rel="stylesheet"/>

<input type="button" id="refresh" value="ButtonClick_LightGallery"/>

<div class="demo">
    <ul id="lightSlider">
    </ul>
</div>

<div class="demo-pageload">
    <ul id="lightSlider-pageload">
  
</ul>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Improving the method of scheduling cron jobs using job sequences determined by a PHP script

Recently, I developed a basic video creation script using NodeJS. This script is configured to run on a scheduled cron job. In the user panel built with PHP, users input their details and click the "Submit new Video Job" Button. These details are then sa ...

Header input for searching in a different section of the page

I am working on a project where I have a Header component containing an Input element. My goal is to search within an array located in another component, specifically the product list component. Both of these components are connected through App.js. How ...

When clients format datetime, the default date value is sent on the form post

I'm attempting to utilize this particular example in order to correctly implement datetime formatting In my view model, I have a property for datetime public class MyViewModel { [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0 ...

Enable Auto-Play for HTML5 Videos Inside Fancybox

I am currently working on a project that utilizes Fancybox for presenting individual HTML5 Videos and HTML5 video galleries. My goal is to have the single videos autoplay when opened in Fancybox, and then automatically close Fancybox once the video has fi ...

Exploring Angular $resource with a playful twist

Is there a recommended method for mocking the $resource object? I've looked online, but all my attempts ended with KARMA testing. It's not what I'm looking for. I'm thinking of creating a fake object so that I can easily switch betwee ...

Encountering a 'ag-grid: Component [agSetColumnFilter] not found' error while utilizing an Ag-Grid Enterprise license

For several months, I've been successfully utilizing Ag-Grid's Enterprise feature called "agSetColumnFilter". The versions in use are: "ag-grid": "^17.1.1", "ag-grid-enterprise": "^17.1.1", "ag-grid-react": "^17.1.0", Recently, aft ...

Mocking a React component with Jest's MockImplementation

Currently, I am in the process of testing a react component that renders another component. This secondary component makes an API call to fetch data which is then displayed on the screen. My goal is to understand how I can mock this particular component&ap ...

Tooltips that appear when hovering over elements in a Fixed Data Table

I am looking to incorporate both click and hover events for tooltip content within the cells of the Fixed Data Table component. Currently, I am utilizing react-tippy for tooltips with the use of the html option to create a dropdown menu. While I can displa ...

Platform providing video recommendations based on user preferences

Assigned with creating a unique online video lecture platform inclusive of a recommendation system for the students in my area, I have limited knowledge on html, css, java, and php as my education primarily came from YouTube tutorials. Confused about what ...

Activate a jQuery collapsible feature through an external hyperlink

Can we enable the expansion of a jQuery collapse by clicking on an external link? For instance, let's say we have a link on the home page that leads to another page. When the user clicks on this link from the home page, we want it to redirect to the i ...

Fetching only the body content when making an Ajax request with CakePHP

I have implemented a modal dialog that prompts users to change their passwords under certain conditions. While the functionality works perfectly, I encounter an issue where upon completion of the password change request, the entire page along with the tem ...

Replicate the process of transferring table rows to the clipboard, but exclusively copying the contents

Currently, I am attempting to copy a paginated table to my clipboard by referring to this guide: Select a complete table with Javascript (to be copied to clipboard). However, the issue lies in the fact that it only copies the data from the first page. In ...

You cannot use objects as a React child within the react-stacked-center-carousel component

Using the react-stacked-center-carousel, I have encountered an issue with passing an array of objects to the StackedCarousel component. Here is the link to my reference sandbox: Reference Sandbox I also have a sandbox where I am trying to implement it mys ...

JQuery ID Media Queries: Enhancing responsive design with targeted

Is it possible to integrate a media query into the selection of an ID using JQuery? For example: $('#idname') $('@media (max-width: 767px) { #idname }') In essence, can you target the #idname with that specified media query in JQuery ...

Configuring a JavaScript calendar with custom margins

Having trouble selecting a date with the Bootstrap datepicker class. After running the snippet, the calendar appears below the textbox: <input type="text" class="form-control datepicker" name="due_date" id="due_date" onclick="calendar_up()" value="" pl ...

jQuery does not support displaying output using console.log

I have a JavaScript code that uses jQuery. However, when I click the #button_execute button, the console.log inside the callback function of .done doesn't display anything on the console. I'm not sure how to troubleshoot this issue. $("#button_e ...

Dividing a string by a specified array element in Javascript

Given a string "asdlfjsahdkljahskl" and an array [1,2,3,1,7,2,1,2], the final output should be a, sd, lfj, s, ahdklja, hs, kl. I'm able to split the string, but I'm unsure how to compare the string and cut it according to the given array. Here& ...

Show the layout of the table in a visual format

I am struggling to showcase a table created using <ul> tags. I want the content to be displayed one after the other. Here is my code: CSS .activity-list-header > li { display: inline-block; text-align: left; width: 15.666%; list- ...

Monitor Jenkins live logs with a personalized dashboard for real-time viewing

Our Jenkins CE is currently running close to 4000 jobs, with users accessing a Dashboard that utilizes Jenkins APIs. Previously, we provided an href link with a logs button for viewing logs, which would open the Jenkins logs page in an iFrame when clicked. ...

What is the best way to place an icon in the center of a Bootstrap 5 card image?

I'm looking to center an icon within a bootstrap-5 card image. I have an icon that I want to display at the center of my card image, but I'm unsure how to achieve this using either CSS or Bootstrap-5. Can anyone provide guidance on how to accompl ...