I want to create simple product slider with thumbnails. Although there may be more than 3 thumbnails, I want to show only 3 of them. By clicking 'more' button i am aiming to hide first image which is already visible and show one from hidden images by changing their class.
Code works fine untill hiddenImages[0]. Firefox console gives the following error: "TypeError: hiddenImages[0] is undefined"
What i am doing wrong?
// All images
var images = $('[data-image]');
// Click for more images
var more = $('.more');
// Add show class to all images
images.each(function(index, element){$(this).parent().addClass('visible')})
// Hide images begining from 4th image
images.each(function(index, element){if($(this).data('image') >= 4)
{$(this).parent().removeClass('visible').addClass('hidden')} })
// Show big image when clicking thumbnail
images.each( function(index, element){
$(this).click(function(){ $('#pic img').attr('src', $(this).attr('src')) }) })
// Hide 1st from visible images and show first from hidden images
more.on('click', function(){
// Find all hidden images and remove visible class from first one
hiddenImages = images.hasClass('hidden');
hiddenImages[0].removeClass('visible').addClass('hidden');
})
#pic {
width: 300px;
height: 300px;
border: 1px solid #ccc;
margin-right: 5px;
float: left;
}
.thumbnails {
height: 300px;
width: 50px;
padding:0;
margin:0;
margin-right: 10px;
float: left;
}
.thumbnails li {
display: inline;
list-style: none;
float: left;
width: 70px;
height: 70px;
margin-bottom: 5px;
border:1px solid #ccc;
text-align: center;
}
.thumbnails li img {
width: 70px;
height: 70px;
cursor: pointer;
}
.more {
display: inline;
list-style: none;
float: left;
width: 70px;
height: 70px;
margin-bottom: 5px;
border:1px solid #ccc;
text-align: center;
}
.hidden {display: none!important;}
.visible {display: block!important;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="holder">
<div id="pic">
<img src="https://placeimg.com/300/300/nature">
</div>
<ul class="thumbnails">
<li><img data-image="1" src="https://placeimg.com/300/300/nature"></li>
<li><img data-image="2" src="https://placeimg.com/300/300/any"></li>
<li><img data-image="3" src="https://placeimg.com/300/300/animals"></li>
<li><img data-image="4" src="https://placeimg.com/300/300/sepia"></li>
<li><img data-image="5" src="https://placeimg.com/300/300/grayscale"></li>
<li><img data-image="6" src="https://placeimg.com/300/300/tech"></li>
<li class="more">MORE</li>
</ul>
</div>
.get(0)instead of[0]if you want the actual DOM element. If you still want the jQuery wrapped element, use.first().hasClass()returns a boolean.