0

My image link sources in for loop

  foreach (images as image) { 

 echo ?> <a id="image" onclick="return false;" href=" my image link"  >
          <span > <?php  echo  $image->imageName; ?></span> <?
    }

Output for image link something like Link1 Link2 Link3

HTML to show image

<img id="copyimage" src="" alt="No Pic" style="width:304px;height:228px;">

My Jquery

(document).ready(function(){
    $("#image").click(function(){

  var link = $(this).attr('href');

    $("#copyimage").attr("src", link);

});
});

when I click to each link (Link1 Link2 Link3) ,I want to copy each of them into image source to show image. But it just only worked for first link (Link1) and Link2 Link2 didn't.

jenii
  • 57
  • 1
  • 11

1 Answers1

0

all the link have the some ID id="image" you need to use class :

foreach (images as image) { 

 echo ?> <a class="image" onclick="return false;" href=" my image link"  >
          <span > <?php  echo  $image->imageName; ?></span> <?
    }

Jquery

(document).ready(function(){
        $(".image").click(function(){

      var link = $(this).attr('href');

        $("#copyimage").attr("src", link);

    });
Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28