views:

251

answers:

1

I'd like to click on a trigger and show a specific image. There are multiple triggers which would show a specific image related to it within a set. There are 4 sets The challenge for me is toggling the other images to hide only in this 'set' when one of these triggers are clicked, as there can only be one image showing at a time in each set.

Here is the HTML I've put together thus far:

<!-- Thumbnails which can be clicked on to toggle the larger preview image -->


        <div class="materials">
    <a href="javascript:;" id="shirtgrey"><img src="/grey_shirt.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="shirtred"><img src="red_shirt.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="shirtblue"><img src="hblue_shirt.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="shirtgreen"><img src="green_shirt.png" height="122" width="122" /></a> 
    </div>


    <div class="collars">
    <a href="javascript:;" id="collargrey"><img  src="grey_collar.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="collarred"><img  src="red_collar.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="collarblue"><img  src="blue_collar.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="collargreen"><img  src="green_collar.png" height="122" width="122" /></a>
    </div>

    <div class="cuffs">
    <a href="javascript:;" id="cuffgrey"><img  src="grey_cuff.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="cuffred"><img  src="red_cuff.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="cuffblue"><img  src="blue_cuff.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="cuffgreen"><img  src="/green_cuff.png" height="122" width="122" /></a>
    </div>

    <div class="pockets">
    <a href="javascript:;" id="pocketgrey"><img  src="grey_pocket.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="pocketred"><img  src=".png" height="122" width="122" /></a> 
    <a href="javascript:;" id="pocketblue"><img  src="blue_pocket.png" height="122" width="122" /></a> 
    <a href="javascript:;" id="pocketgreen"><img  src="green_pocket.png" height="122" width="122" /></a>
    </div>

<!-- The larger images where one from each set should be viewable at one time, triggered by the thumb clicked above -->


        <div class="selectionimg"> 
        <div class="selectShirt">
        <img src="grey_shirt.png" height="250" width="250" class="selectShirtGrey show" />        
        <img src="red_shirt.png" height="250" width="250" class="selectShirtRed hide" />        
        <img src="blue_shirt.png" height="250" width="250" class="selectShirtBlue hide" />        
        <img src="green_shirt.png" height="250" width="250" class="selectShirtGreen hide" />  </div>

         <div class="selectCollar">
        <img src="grey_collar.png" height="250" width="250" class="selectCollarGrey show" />        
        <img src="red_collar.png" height="250" width="250" class="selectCollarRed hide" />        
        <img src="blue_collar.png" height="250" width="250" class="selectCollarBlue hide" />        
        <img src="green_collar.png" height="250" width="250" class="selectCollarGreen hide" />       </div>

         <div class="selectCuff">
        <img src="grey_cuff.png" height="250" width="250" class="selectCuffGrey show" />        
        <img src="red_cuff.png" height="250" width="250" class="selectCuffRed hide" />        
        <img src="blue_cuff.png" height="250" width="250" class="selectCuffBlue hide" />        
        <img src="green_cuff.png" height="250" width="250" class="selectCuffGreen hide" />     </div>

         <div class="selectPocket">
        <img src="grey_pocket.png" height="250" width="250" class="selectPocketGrey show" />        
        <img src="hred_pocket.png" height="250" width="250" class="selectPocketRed hide" />        
        <img src="blue_pocket.png" height="250" width="250" class="selectPocketBlue hide" />        
        <img src="green_pocket.png" height="250" width="250" class="selectPocketGreen hide" />   
    </div>     </div>

How can jQuery be used to change a class of an image to "show" and ensure that all other images in that same div are set to a class of "hide"?

First time posting here. I'm very efficient with HTML and CSS and have a basic understanding of jQuery. I'm learning and this just seems a little bit beyond my abilities at the moment.

I hope this all makes sense. Thanks for any help.

A: 

There is a toggle() method for jquery. But before I go look that up, you could do this long hand like this:

First off, your div classes are not classes, from what I can tell, but id's, as they are unique to the set. So you should make it

<div id="cuffs" class="set">

Then you can do this with jquery:

$(".set a").click(function() {
    $(this).parent("img").removeClass("visible");
    $("img",this).addClass("visible");
});

Like I said, there is probably a slicker way with toggle(), but basically, the outer selector has the onclick handler. The first line of the function says to select all images that are inside of the outer div (the parent) and remove the class "visible". The second line says to add the class "visible" to the image inside of the clicked link.

This way, instead of having a "hidden" class and a "visible" class, you can just have all images within a "sets" div have a default css rule:

 .sets img [
      display: none;
 }

and then have another css rule for visible images:

 .sets img.visible {
       display: block;
 }

And so instead of worrying about toggling, you just strip all of the images in the div of the class (whether they have it or not), and then re-add it to the one that needs it.

Anthony
You are on the right track for what I need however the image being clicked to show/hide is in a completely different div then where the actual show/hide is occuring and not necessarily the parent div. Essentially I need to be able to trigger this event from a thumb in a div that is located anywhere on the page. Thanks again for your help. I feel like I'm almost there.
liquilife