Add data attribute for all images under class

I use a lightbox that requires a specific tag inside the link. I don't want to edit every post, so I'm trying to do this automatically using jquery.

HTML

<div class="wrapper">
   <a href="imagelink.png">Image</a>
</div>

Js

  $(document).ready(function() {

// Scan classes
$('.wrapper a').each(function(){

    // Apply tag
    $(this).parent().attr('data-lity');

});
});

The result should be

<div class="wrapper">
   <a href="imagelink.png" data-lity>Image</a>
</div>

JSfiddle http://jsfiddle.net/c2RvG/31/

+4
source share
3 answers

It will do the trick

Jquery

$(document).ready(function() {

    // Scan the webpage for all class names of 'thumb' that is seen.

    $('.wrapper a').attr('data-lity', '');

});

enter image description here

Demo

+1
source

script

$('.wrapper a').attr('data-lity', '');

You do not need iteration. JQuery does it for you. Just set the attribute.

Hope this helps. Hooray!

+3
source

  $(document).ready(function() {

 // Scan classes
   $('.wrapper a').each(function(){

 // Apply tag
    $(this).parent().attr('data-lity','');

  });
 });
0

Source: https://habr.com/ru/post/1652784/


All Articles