views:

112

answers:

7

I have 13 small pictures and 13 big pictures,if any of the small pictures are clicked it'll show the related big picture, I was just wondering if it was possible to generalize the click function so I don't have to repeat the dame code 13 times, thanks

<div id="press_images">

                <img id="s1" class="small" src="images/press/small/1.png" />
                <img id="s2" class="small" src="images/press/small/2.png" />
                <img id="s3" class="small" src="images/press/small/3.png" />
                <img id="s4" class="small" src="images/press/small/4.png" />

                .....
                <img id="s13" class="small" src="images/press/small/13.png" />
</div>

<div class="big">     
               <a id="close">X</a>
               <img id="b1" src="images/press/big/1.jpg" />
               ......
               <img id="b13" src="images/press/big/13.jpg" />
</div>


$("#s1").click(function(){  

  $('#b1').show();
  $('.big').show(300);
      return false;  
   });

so I was wondering if I could change the $("#s1").click(function() so I don't have to repeat it 13 times.

thanks

A: 

Bind click() event to a list of elements or to their parent.

hsz
Hi, thanks for the reply,but how can I do that?
amir
`$("#press_images > img").click(function(){ ... });`
hsz
That doesn't generalize the callback
Justin Johnson
+1  A: 

Try this

// For all <img>'s with class `small`
$("img.small").click(function() {
    // Get the index from the small image's ID
    var index = this.id.substr(1);
    // Hide all other big images
    $(".big img").hide()
    // Show the related big images
    $('#b' + index).show();
    // Show the big image container
    $('.big').show(300);

    return false;
});
Justin Johnson
A: 

Put a class in every image. for example:

<img id="s1" class="small clickable-image" src="images/press/small/1.png" />

And then:

$(".clickable-image").click(function(){  

  $('#b1').show();
  $('.big').show(300);
  return false;  
});

EDIT: then of course, you have to change the line $('#b1').show(); but, it's easy, you just have to get the element ID and build the ID of the bigger image. Just like Justin Johnson does in his answer.

Drevak
In that case b1 will show everytime as a big image.
Guillaume Flandre
That doesn't generalize the callback
Justin Johnson
Yup, i just copy-pasted the code. Didn't see it. He just need to do what you put in your answer.
Drevak
+1  A: 

The following should work :

$(".small").click(function(){  
      var id_img=$(this).attr('id').replace('s','');
      $('.big img').hide();
      $('#b'+id_img).show();
      $('.big').show(300);
      return false;  
});
Guillaume Flandre
A: 
<div id="press_images">

                <img id="small1" class="small small-img"  src="images/press/small/1.png" />
                <img id="small2" class="small small-img" src="images/press/small/2.png" />
                <img id="small3" class="small small-img" src="images/press/small/3.png" />
                <img id="small4" class="small small-img" src="images/press/small/4.png" />

                .....
                <img id="small13" class="small small-img" src="images/press/small/13.png" />
</div>

<div class="big">     
               <a id="close">X</a>
               <img id="big1" class="big-img" src="images/press/big/1.jpg" />
               ......
               <img id="big13" class="big-img" src="images/press/big/13.jpg" />
</div>


$(".small-img").click(function(e){ 

  var imgBig = String($(this).attr("id")).replace("small", "big");

  e.stopPropagation();
  $(".big-img").hide();
  $("#" + imgBig ).show();
  $(".big").show(300);

});
andres descalzo
The new class and explicit string conversion aren't necessary. But good call on `$(".big-img").hide();`
Justin Johnson
+1  A: 

That's how I'd do it:

<div id="press_images">
      <img id="s1" rel="b1" class="small" src="images/press/small/1.png" />
      <img id="s2" rel="b2" class="small" src="images/press/small/2.png" />
</div>

<div class="big">     
      <a id="close">X</a>
      <img id="b1" class="big" src="images/press/big/1.jpg" />
      <img id="b2" class="big" src="images/press/big/2.jpg" />
</div>


$(".small").click(function(){  
  $( ".big img" ).hide();
  $( "#"+ $(this).attr("rel") ).show();
}

Notice that I use "rel" to link the elements. I consider it to be cleaner than assuming that b1 is related to s1. I like CoC, but I'm not sure that in that case it'd be the best idea.

marcgg
You're not displaying the image anymore. But rel is a good way to do it too.
Guillaume Flandre
I think I fixed it (see edit)
marcgg
You forgot $('.big').show(300); but it's no big deal, the essential is here.
Guillaume Flandre
A: 

The most flexible option is to use index and eq and get rid of all explicit html helpers, like ID or rel

$('#press_images img').click(function() {
 var n = $('#press_images img').index(this);
 $(".big img").hide().eq(n).show()
});
stereofrog