0

I'm trying to wrap() the .gallery-items with the text() that is an url from the .gallery-caption. My problem is that all the urls are fetched and inserted into the a href"=https://s.

What would be the proper way to make it work correctly? Thank you in advance.

Below is my attempt:

$(".gallery-item").wrap($("<a/>").attr("href", "http://"+$(".gallery-caption").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><figure class="gallery-item">
            <div class="gallery-icon landscape">
                <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/bosch.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-42">
            </div>          
      

I also tried to implement the $(this)... but I might have it used wrongly.

iorgv
  • 787
  • 2
  • 11
  • 26

1 Answers1

1

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>
Swati
  • 28,069
  • 4
  • 21
  • 41
  • How would it be possible to use this code when the html is loaded with jQuery dynamically? [link](https://stackoverflow.com/questions/70812535/wrap-is-not-working-on-dynamically-loaded-element-why) – iorgv Jan 23 '22 at 13:47