I have a slider on the page that switches images
<div class="custom-carousel-section">
<div class="custom-container">
<div class="custom-carousel">
@if(!empty($article_block_images))
@foreach($article_block_images as $article_block_image)
<div class="custom-carousel__title">
@if($loop->first)
<span>{{ $article_block_image->title }}</span>
@endif
</div>
@endforeach
@endif
@if(!empty($article_block_images))
@foreach($article_block_images as $article_block_image)
<div class="main-image" style="display: {{ $loop->first ? 'block' : 'none' }}">
<picture>
<source srcset="{{ $article_block_image->mobile_image }}" media="(max-width: 576px)" alt="{{ $article_block_image->image_alt }}" title="{{ $article_block_image->image_title }}" data-title="{{ $article_block_image->title }}">
<source srcset="{{ $article_block_image->main_image }}" alt="{{ $article_block_image->image_alt }}" title="{{ $article_block_image->image_title }}" data-title="{{ $article_block_image->title }}">
<img src="{{ $article_block_image->main_image }}" alt="{{ $article_block_image->image_alt }}" title="{{ $article_block_image->image_title }}" data-title="{{ $article_block_image->title }}">
</picture>
</div>
@endforeach
@endif
<div class="img-to-select">
@if($articleBlockImagesCount > 1)
@if(!empty($article_block_images))
@foreach($article_block_images as $article_block_image)
<div @if($loop->first) class="img-to-select__item selected" @else class="img-to-select__item" @endif>
<img src="{{ $article_block_image->preview_image }}" alt="{{ $article_block_image->image_alt }}" title="{{ $article_block_image->image_title }}" data-title="{{ $article_block_image->title }}" data-main-src="{{ $article_block_image->main_image }}" data-mobile-src="{{ $article_block_image->mobile_image }}">
</div>
@endforeach
@endif
@endif
</div>
</div>
</div>
</div>
$('.img-to-select__item').click(function () {
$('.img-to-select__item').removeClass('selected');
$(this).addClass('selected');
let index = $(this).index();
$('.main-image').hide();
$('.main-image').eq(index).show();
$('.custom-carousel__title > span').html($(this).children('img').attr('data-title'));
});
It works when there is only one such slider on the page, here is an example
https://jsfiddle.net/a9yvdu75/
Here the code is slightly different from mine, but in general the principle is the same
When there are two sliders on the page, then everything does not work correctly, here is an example
https://jsfiddle.net/9std8k3u/
How can this be fixed?
I tried this
$('.img-to-select__item').click(function () {
let currentSelection = $(this);
$('.img-to-select__item').removeClass('selected');
$(currentSelection).addClass('selected');
let index = $(currentSelection).index();
$(currentSelection).closest('.img-to-select').prevAll('.main-image').hide();
$(currentSelection).closest('.img-to-select').prevAll('.main-image').eq(index).show();
$(currentSelection).closest('.img-to-select').prevAll('.custom-carousel__title > span').find('span').html($(currentSelection).children('img').attr('data-title'));
});
Everything seems to work, but when you select a preview picture, the main one displays not the one you need, but the neighboring one