Nick Scola
Nick Scola

Reputation: 3

jQuery switching images when link is clicked

Need some help here. I'm trying to achieve this:

By default the images are hidden by css display: none;

When the user clicks on the link for example: vanilla glazed it will display the image that matches the image alt "vanilla-glazed".

Then if the user clicks on another link for example: brookyln blackout it will hide the active image and show the brookyln blackout image.

Makes sense? Here's my html markup.

I need help with the jQuery part. Not sure where to start.

<div id="nameWrapper">

<ul>

<li><a href="#" class="vanilla-glazed">vanilla glazed</a></li>
<li><a href="#" class="brookyln-blackout">brookyln blackout</a></li>

</ul>

</div>


<div class="gallery">

<div class="image-wrapper">

<img src="#" alt="brookyln-blackout" />

   <div class="meta">
   <p>description</p>
   </div>

</div>

<div class="image-wrapper">

<img src="#" alt="vanilla-glazed" />

   <div class="meta">
   <p>description</p>
   </div> 

</div>

</div>

Upvotes: 0

Views: 124

Answers (1)

benzonico
benzonico

Reputation: 10833

You can try something like this

 $(document).ready(function(){ //on load of the document 
   $("#nameWrapper a").click(function(){ //on a click on a a in your div
       $('.image-wrapper img').hide(); //hide all images (including already hidden ones
       $("img[alt='"+$(this).attr('class')+"']").show(); //show the image with alt value          
   });
});

see fiddle

Upvotes: 1

Related Questions