Vianne
Vianne

Reputation: 578

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/

Upvotes: 5

Views: 1859

Answers (3)

Fiido93
Fiido93

Reputation: 1978

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

Upvotes: 2

hdotluna
hdotluna

Reputation: 5732

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!

Upvotes: 3

Try this

  $(document).ready(function() {

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

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

  });
 });

Upvotes: 0

Related Questions