JQuery – Gallery caption text as link to wrap gallery items

Solution:

You can simply use each loop and then inside this loop use this which will refer to current element which is iterated .

Demo Code :

$(".gallery-item").each(function() {
  $(this).wrap($("<a/>").attr("href", "http://" + $(".gallery-caption", this).text().trim()));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<figure class="gallery-item">
  <div class="gallery-icon landscape">
    <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/areklam.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-39">
  </div>
  <figcaption class="wp-caption-text gallery-caption" id="gallery-1-39">
    www.areklam.hu
  </figcaption>
</figure>
<figure class="gallery-item">
  <div class="gallery-icon landscape">
    <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/arkad.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-40">
  </div>
  <figcaption class="wp-caption-text gallery-caption" id="gallery-1-40">
    www.arkadbudapest.hu
  </figcaption>
</figure>
<figure class="gallery-item">
  <div class="gallery-icon landscape">
    <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/besttv.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-41">
  </div>
  <figcaption class="wp-caption-text gallery-caption" id="gallery-1-41">
    www.besttv.hu
  </figcaption>
</figure>