Vianne Yu Zhèng Vianne Yu Zhèng - 3 months ago 14
Javascript Question

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/

Answer

This will do a trick

Jquery

$(document).ready(function() {

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

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

});

enter image description here

DEMO