Husna
Husna

Reputation: 1386

onClick current div display using jquery

I'm working on Image gallery here I want onClick image current image display in .gallery-container which is working fine but the problem is current image caption is not displaying because I target the image source in jquery I tried to add caption also but unable to achieve. Can anyone point me in the right direction.

$(document).ready(function() {
  $('.gallery-column img').on('click', function() {
    var expandImg = document.getElementById("expandedImg");
    expandImg.src = this.src;
    expandImg.parentElement.style.display = "block";
  });
});
* {
  box-sizing: border-box;
}

.gallery-wrap {
  width: 50%;
  height: 1066px;
  margin: 0 auto;
  display: flex;
}

.gallery-row {
  width: 52%;
  max-height: 497px;
}

.gallery-column {
  position: relative;
  padding: 10px;
}

.img-caption {
  position: absolute;
  bottom: 10%;
}

.gallery-column img {
  width: 100%;
  opacity: 0.8;
  cursor: pointer;
}

.gallery-column img:hover {
  opacity: 1;
}

.gallery-container {
  position: relative;
  width: 90%;
  height: 500px;
}

#expandedImg {
  width: 100%;
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gallery-wrap">

  <div class="gallery-container" style="display: block;">
    <img id="expandedImg" src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60">
    <div class="img-caption">
      <h3>Still more than 2 Millions+ people using</h3>
    </div>
  </div>

  <div class="gallery-row">
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>Still more than 2 Millions+ people using</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542646822891-0a8451fce513?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=181de57e88385f8f32f48aef4e2831f9&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>olor sit amet, consectetur adipiscing elit. Donec</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542632890661-441e6f424098?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=60444a082924abcdc72901abaa800620&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>sit amet, consectetur adipiscing elit. Donec</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542635521008-f80f4d69bad4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=a6e7baa2e8b1e236090d116d35bf0855&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>nsectetur adipiscing elit. Donec</h3>
      </div>
    </div>

    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542567055-2c294d7201bd?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=9d4553e0dddb5dfc9e8c2851c24e4610&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>consectetur adipiscing elit. Donec</h3>
      </div>
    </div>


  </div>
</div>

Upvotes: 0

Views: 70

Answers (3)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48683

You can turn this into a jQuery plugin and remove the ID. This way, you can have various galleries on one page. I would target the img and .img-caption selectors and modify their attributes.

(function($) {
  $.fn.child = function(s) {
      return $(this).children(s).first();
  };
  $.fn.gallery = function() {
    var $this = this;
    $this.find('.gallery-column').on('click', function(e) {
      var $container = $this.find('.gallery-container');
      $container.child('.gallery-display').prop('src', $(this).child('img').prop('src'));
      $container.child('.img-caption').html($(this).child('.img-caption').html());
    });
    return $this;
  };
})(jQuery);

$(document).ready(function() {
  $('.gallery-wrap').gallery(); // Convert to gallery.
});
* {
  box-sizing: border-box;
}

.gallery-wrap {
  width: 75%;
  height: 1066px;
  margin: 0 auto;
  display: flex;
}

.gallery-row {
  width: 52%;
  max-height: 497px;
}

.gallery-column {
  position: relative;
  padding: 10px;
}

.img-caption {
  position: absolute;
  margin: 0 2%; /* Added padding to the left and right */
  bottom: 10%;
}

.gallery-column img {
  width: 100%;
  opacity: 0.8;
  cursor: pointer;
}

.gallery-column img:hover {
  opacity: 1;
}

.gallery-container {
  display: block;
  position: relative;
  width: 90%;
  height: 500px;
}

.gallery-display {
  width: 100%;
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gallery-wrap">
  <div class="gallery-container">
    <img class="gallery-display" src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60">
    <div class="img-caption">
      <h3>Still more than 2 Millions+ people using</h3>
    </div>
  </div>
  <div class="gallery-row">
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>Still more than 2 Millions+ people using</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542646822891-0a8451fce513?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=181de57e88385f8f32f48aef4e2831f9&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>olor sit amet, consectetur adipiscing elit. Donec</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542632890661-441e6f424098?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=60444a082924abcdc72901abaa800620&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>sit amet, consectetur adipiscing elit. Donec</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542635521008-f80f4d69bad4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=a6e7baa2e8b1e236090d116d35bf0855&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>nsectetur adipiscing elit. Donec</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542567055-2c294d7201bd?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=9d4553e0dddb5dfc9e8c2851c24e4610&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>consectetur adipiscing elit. Donec</h3>
      </div>
    </div>
  </div>
</div>

Upvotes: 1

Aquib Iqbal
Aquib Iqbal

Reputation: 375

Just add below code you image click function.

var _caption = $(this).next("div").html();
$("#expandedImgCaption").html(_caption);

expandedImgCaption is the id for caption for expanded image

Code Snippet :

$(document).ready(function() {
  $('.gallery-column img').on('click', function() {
    var expandImg = document.getElementById("expandedImg");
    expandImg.src = this.src;
    expandImg.parentElement.style.display = "block";
    var _caption = $(this).next("div").html();
    $("#expandedImgCaption").html(_caption);
  });
});
* {
  box-sizing: border-box;
}

.gallery-wrap {
  width: 50%;
  height: 1066px;
  margin: 0 auto;
  display: flex;
}

.gallery-row {
  width: 52%;
  max-height: 497px;
}

.gallery-column {
  position: relative;
  padding: 10px;
}

.img-caption {
  position: absolute;
  bottom: 10%;
}

.gallery-column img {
  width: 100%;
  opacity: 0.8;
  cursor: pointer;
}

.gallery-column img:hover {
  opacity: 1;
}

.gallery-container {
  position: relative;
  width: 90%;
  height: 500px;
}

#expandedImg {
  width: 100%;
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gallery-wrap">

  <div class="gallery-container" style="display: block;">
    <img id="expandedImg" src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60">
    <div id="expandedImgCaption" class="img-caption">
      <h3>Still more than 2 Millions+ people using</h3>
    </div>
  </div>

  <div class="gallery-row">
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>Still more than 2 Millions+ people using</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542646822891-0a8451fce513?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=181de57e88385f8f32f48aef4e2831f9&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>olor sit amet, consectetur adipiscing elit. Donec</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542632890661-441e6f424098?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=60444a082924abcdc72901abaa800620&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>sit amet, consectetur adipiscing elit. Donec</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542635521008-f80f4d69bad4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=a6e7baa2e8b1e236090d116d35bf0855&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>nsectetur adipiscing elit. Donec</h3>
      </div>
    </div>

    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542567055-2c294d7201bd?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=9d4553e0dddb5dfc9e8c2851c24e4610&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>consectetur adipiscing elit. Donec</h3>
      </div>
    </div>


  </div>
</div>

Upvotes: 1

Harish Soni
Harish Soni

Reputation: 1896

This one is working as expected

$(document).ready(function() {
  $('.gallery-column').on('click', function() {
    var expandImg = document.getElementById("hoc");
    expandImg.replaceChild(this, expandImg.childNodes[0])
  });
});
* {
  box-sizing: border-box;
}

.gallery-wrap {
  width: 50%;
  height: 1066px;
  margin: 0 auto;
  display: flex;
}

.gallery-row {
  width: 52%;
  max-height: 497px;
}

.gallery-column {
  position: relative;
  padding: 10px;
}

.img-caption {
  position: absolute;
  bottom: 10%;
}

.gallery-column img {
  width: 100%;
  opacity: 0.8;
  cursor: pointer;
}

.gallery-column img:hover {
  opacity: 1;
}

.gallery-container {
  position: relative;
  width: 90%;
  height: 500px;
}

#expandedImg {
  width: 100%;
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gallery-wrap">

  <div class="gallery-container" id="hoc" style="display: block;">
    <img id="expandedImg" src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60">
    <div class="img-caption">
      <h3>Still more than 2 Millions+ people using</h3>
    </div>
  </div>

  <div class="gallery-row">
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>Still more than 2 Millions+ people using</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542646822891-0a8451fce513?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=181de57e88385f8f32f48aef4e2831f9&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>olor sit amet, consectetur adipiscing elit. Donec</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542632890661-441e6f424098?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=60444a082924abcdc72901abaa800620&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>sit amet, consectetur adipiscing elit. Donec</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542635521008-f80f4d69bad4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=a6e7baa2e8b1e236090d116d35bf0855&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>nsectetur adipiscing elit. Donec</h3>
      </div>
    </div>

    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542567055-2c294d7201bd?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=9d4553e0dddb5dfc9e8c2851c24e4610&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>consectetur adipiscing elit. Donec</h3>
      </div>
    </div>


  </div>
</div>

Upvotes: 1

Related Questions