I am trying to create very simple image browser with jQuery. I am doing it only for learning, it will not be used in any project at this time.
I have images and thumbnails as follows:
<div class="images">
<img src="http://www.dummyimage.com/400x200/222/fff.jpg" alt="" data-idi="1"/>
<img src="http://www.dummyimage.com/400x200/534/fff.jpg" alt="" data-idi="2"/>
<img src="http://www.dummyimage.com/400x200/345/fff.jpg" alt="" data-idi="3"/>
<img src="http://www.dummyimage.com/400x200/fed/111.jpg" alt="" data-idi="4"/>
</div>
<div class="thumbs">
<button data-idb="1"><img src="http://www.dummyimage.com/50x50/222/fff.jpg" alt="" /></button>
<button data-idb="2"><img src="http://www.dummyimage.com/50x50/534/fff.jpg" alt="" /></button>
<button data-idb="3"><img src="http://www.dummyimage.com/50x50/345/fff.jpg" alt="" /></button>
<button data-idb="4"><img src="http://www.dummyimage.com/50x50/fed/111.jpg" alt="" /></button>
</div>
Hovering on thumbnail should replace main image above.
I have managed to do it with one, let’s call it ”set” of images, as presented on jsfiddle.net.
Here is simple script used to achieve this:
$('#images img').hide().first().show();
$('#thumbs button').on( "mouseover", function(e) {
var idb = $(this).data("idb");
$('#images img').hide();
var img = $('#images').find("[data-idi='" + idb + "']").show();
});
Now I want to add another set of images, and wrap the script with .each() function. But I am having a problem on very begining, when I am trying to show only first image:
var imgs = $('.images').hide();
$.each(imgs, function( index, val ) {
var firstImage = $(this).children(":first");
console.log(firstImage);
firstImage.show();
});
Proper image is being consoled, but it is not showing, as excepted. Live example on jsfiddle.net. Could you help me with this problem? TIA
You’ve already hidden the parent
.imageselement.After calling
.show(), you have a visible element inside a hidden parent.You need to select the
<img>elements themselves by writing$('.images img').