views:

40

answers:

1

Hi everyone,

I need some help in jQuery. What I want to do is to create some kind of a small, very, very easy picture gallery.

I want to have a couple of small pictures on the left-hand-side of my page. By clicking one of these small pictures I'd like to load this picture into another div where it is shown in full size.

<a href="image1_big.jpg" class="smallpics" /><img src="image1_small" /></a>
<a href="image2_big.jpg" class="smallpics" /><img src="image2_small" /></a>
<a href="image3_big.jpg" class="smallpics" /><img src="image3_small" /></a>
<div id="bigpic" /><img src="image1_big" /></div>

All my jQuery attemts failed as I could not change the source of the image within this bigpic div.

+1  A: 

This should do it for you:

$(function(){
   $("a.smallpics").click(function(e){
      $("#bigpic").html('<img src="' + this.href + '" />');
      e.preventDefault();
   });
});

I could have also used this (instead of the similar line in the previous example):

$("#bigpic").empty().append($("<img />").attr('src', this.href));

But the first option will actually execute it tiny bit faster. With only one call you won't be able to tell the difference, so if option two is easier to read or write, you can use that.

Doug Neiner
Hey dcneiner thx for this super fast answer, but somehow the e.preventDefault() seems not to work. If I click on a small image my browser (firefox) opens the picture and does not stick to my html site.
Niko
@Niko, it does not look like you pasted your real HTML code in your question. Is it possible for you to copy exactly what you have and edit your original question to show that? It is possible you have something else going on.
Doug Neiner
Ah, I have an idea... I updated my question, just try the new code.
Doug Neiner
Thx so much that work perfect!
Niko
@Niko! Awesome! Be sure to mark the answer as correct. Glad it worked for you!
Doug Neiner