Vianne Yu Zhèng Vianne Yu Zhèng - 1 year ago 65
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.


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


$(document).ready(function() {

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

// Apply tag


Result should be

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


Answer Source

This will do a trick


$(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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download