NO BUG: Nestable Slider: Customization via JavaScript

Browser: Chrome 110
OS: macOS / Windows / Linux / etc.
URL: Link to a page that illustrates this issue
Video: Jam

I wanted to control my sliders with self-made buttons. The instructions from the Bricks Academy were very helpful in getting the first slider to work. Unfortunately, the instructions do not work if more than one slider is used on the page. For some reason, only the top buttons are recognised and then control both sliders … that’s strange.

The adapted code:

<script>
document.addEventListener('DOMContentLoaded', (event) => {
  // Give some times for Bricks to init the sliders
  setTimeout(() => {
    // Please replace rrbqsp to your Slider element ID !!NOT CSS ID!!
    const mySlider = window.bricksData?.splideInstances['xnkdeo'] || false
    // Please replace the button classes to suit your scenario
    const myPrevBtn = document.querySelector('.my-prev')
    const myNextBtn = document.querySelector('.my-next')

    if (mySlider && myPrevBtn && myNextBtn) {
      // Add click event handlers for your custom buttons
      myPrevBtn.addEventListener('click', function () {
        mySlider.go('-1') // go() function by SplideJS
      })
      myNextBtn.addEventListener('click', function () {
        mySlider.go('+1') // go() function by SplideJS
      })
    }
  }, 250)
})
</script>



<script>
document.addEventListener('DOMContentLoaded', (event) => {
  // Give some times for Bricks to init the sliders
  setTimeout(() => {
    // Please replace rrbqsp to your Slider element ID !!NOT CSS ID!!
    const mySlider = window.bricksData?.splideInstances['pkrxhk'] || false
    // Please replace the button classes to suit your scenario
    const myPrevBtn = document.querySelector('.my-prev')
    const myNextBtn = document.querySelector('.my-next')

    if (mySlider && myPrevBtn && myNextBtn) {
      // Add click event handlers for your custom buttons
      myPrevBtn.addEventListener('click', function () {
        mySlider.go('-1') // go() function by SplideJS
      })
      myNextBtn.addEventListener('click', function () {
        mySlider.go('+1') // go() function by SplideJS
      })
    }
  }, 250)
})
</script>

Link to the Bricks Academy instructions:

Link to my site:
https://new.drivers-posting.eu/

Hi,
Thanks so much for your report!

I’m not a JS expert either, but something like this should work (maybe there are better solutions).
You must use different classes for the prev and next links for the respective sliders.

<script>
document.addEventListener('DOMContentLoaded', (event) => {
  // Give some times for Bricks to init the sliders
  setTimeout(() => {
    // Define slider and button classes
    const sliderBtnPairs = [
      { sliderId: 'lghqly', prevBtnClass: '.my-prev1', nextBtnClass: '.my-next1' },
      { sliderId: 'wcfhhn', prevBtnClass: '.my-prev2', nextBtnClass: '.my-next2' }
    ];

    // Iterate over slider-button pairs
    sliderBtnPairs.forEach(pair => {
      // Get slider and button elements
      const mySlider = window.bricksData?.splideInstances[pair.sliderId] || false;
      const myPrevBtn = document.querySelector(pair.prevBtnClass);
      const myNextBtn = document.querySelector(pair.nextBtnClass);

      // Add event listeners if slider and buttons exist
      if (mySlider && myPrevBtn && myNextBtn) {
        myPrevBtn.addEventListener('click', () => mySlider.go('-1'));
        myNextBtn.addEventListener('click', () => mySlider.go('+1'));
      }
    });
  }, 250);
});
</script>

Best regards,
timmse

2 Likes

WOOOOW Thank you so much Timmse!!! It works perfectly :smiley:

Hello Timmse
I have slightly different issue with custom arrow:

  1. I use Nestable Slider for Real Estate projects, as 1 project/ slide, using QUERY Loop
  2. Each project will have (nested) Sliders for project’s images, etc…(Sliders Child)
  3. My Custom arrows use postID dynamic data (of the query) to identify each Sliders child. I put the custom code inside the loop.

4. ISSUE: The custom arrows dont work at all (for Slider child). Although, when I move them out of Parent Slider, it works fine. I guess, there’s sth wrong with my addEventListener → it listen to the Parent Slider, not Child.

Can you help?

Heres how I nest CHILD SLIDER under PARENT SLIDER (and QUERRY)
image

I use PostID to dynamically generate custom arrows

Each child slider has a custom data-script-id
image

Here is the link: Archive – UNICONS - OPTION SLIDE 2B