jquery image gallery is not working as it should

Viewed 80

i am trying to create a simple photo gallery where one photo is big and there are also thumbinals at the bottom which when clicked become that big photo. Galleries are in separate modal windows. My code only works in the first modal window. In fact this is my very first time using jquery. I can't even find how to use the console.log to check where the problem is.

HTML:

  <div class="modal_content">
        <div class="gallery">
            <img id="big" src="foto/20210322_100211.jpg">
        
            <div id="thumbnails">
                <a href="foto/20210218_135910.jpg"><img src="foto/resized/20210218_135910 small.jpg"></a>
                <a href="foto/20210322_100211.jpg"><img src="foto/resized/20210322_100211 small.jpg"></a>
                <a href="foto/20210322_153418.jpg"><img src="foto/resized/20210322_153418 small.jpg"></a>
            </div> 
        </div>
         <!--other content-->
    </div>
    
    <div class="modal_content">
        <div class="gallery">
            <img id="big" src="foto/20210322_153418.jpg">
        
            <div id="thumbnails">
                <a href="foto/20210218_135910.jpg"><img src="foto/resized/20210218_135910 small.jpg"></a>
                <a href="foto/20210322_100211.jpg"><img src="foto/resized/20210322_100211 small.jpg"></a>
                <a href="foto/20210322_153418.jpg"><img src="foto/resized/20210322_153418 small.jpg"></a>
            </div> 
        </div>
         <!--other content-->
    </div>

jQuery:

   $("#thumbnails a").on("click", function (e) {
      e.preventDefault();
    
      $("#big").attr("src", $(this).attr("href"));
    });
2 Answers

id have to be uniq to avoid problem..

eliminate id="big" and replace id="thumbnails" by class="thumbnails"

$(".thumbnails a").on("click", function(e) {
  e.preventDefault();

  $(this).closest(".gallery").find("img").first().attr("src", $(this).attr("href"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="modal_content">
  <div class="gallery">
    <img src="foto/20210322_100211.jpg">

    <div class="thumbnails">
      <a href="foto/20210218_135910.jpg"><img src="foto/resized/20210218_135910 small.jpg"></a>
      <a href="foto/20210322_100211.jpg"><img src="foto/resized/20210322_100211 small.jpg"></a>
      <a href="foto/20210322_153418.jpg"><img src="foto/resized/20210322_153418 small.jpg"></a>
    </div>
  </div>
  <!--other content-->
</div>

<div class="modal_content">
  <div class="gallery">
    <img src="foto/20210322_153418.jpg">

    <div class="thumbnails">
      <a href="foto/20210218_135910.jpg"><img src="foto/resized/20210218_135910 small.jpg"></a>
      <a href="foto/20210322_100211.jpg"><img src="foto/resized/20210322_100211 small.jpg"></a>
      <a href="foto/20210322_153418.jpg"><img src="foto/resized/20210322_153418 small.jpg"></a>
    </div>
  </div>
  <!--other content-->
</div>

explanation:

$(this).closest(".gallery") means find the first parent with class gallery

find("img").first() means find the childrens with tag img and select first


if you replace id="big" by class="big"

you could write too:

$(".thumbnails a").on("click", function(e) {
  e.preventDefault();

  $(this).closest(".gallery").find("img.big").attr("src", $(this).attr("href"));
});

Find closest using jquery closest() and the find #big and replace.

$("#thumbnails a").on("click", function (e) {
    e.preventDefault();
    $(this).closest(".gallery").find(".big").attr("src", $(this).attr("href"));
});
.modal_content{text-align:center;margin-top:100px;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script>
<div class="modal_content">
        <div class="gallery">
            <img class="big" src="https://via.placeholder.com/150">
        
            <div id="thumbnails">
                <a href="https://via.placeholder.com/150"><img src="https://via.placeholder.com/150"></a>
                <a href="https://via.placeholder.com/151"><img src="https://via.placeholder.com/151"></a>
                <a href="https://via.placeholder.com/152"><img src="https://via.placeholder.com/152"></a>
            </div> 
        </div>
         <!--other content-->
    </div>
    
    <div class="modal_content">
        <div class="gallery">
            <img class="big" src="https://via.placeholder.com/150">
        
            <div id="thumbnails">
                <a href="https://via.placeholder.com/150"><img src="https://via.placeholder.com/150"></a>
                <a href="https://via.placeholder.com/151"><img src="https://via.placeholder.com/151"></a>
                <a href="https://via.placeholder.com/152"><img src="https://via.placeholder.com/152"></a>
            </div> 
        </div>
         <!--other content-->
    </div>

Related