Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add data attribute to all images under a class

I'm using a lightbox that requires a specific tag inside the link. I don't really 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');

});
});

Result should be

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

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

like image 649
Vianne Avatar asked Aug 29 '16 03:08

Vianne


2 Answers

script

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

You don't need to iterate. Jquery does it for you. Just set the attribute.

Hope it helps. Cheers!

like image 200
hdotluna Avatar answered Sep 21 '22 22:09

hdotluna


This will do a 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

like image 25
Fiido93 Avatar answered Sep 22 '22 22:09

Fiido93