javascriptjquerylightboxjquery-data

Set data-attribute if contains image as child


To get my lightbox working, I am trying to set the data attribute "lightbox" on links that contain an image as a child. This doesn't seem to work too well. I have this fiddle.

var links = $(".single .entry-content a");
links.each(function () {
    if ($(this).children("img").length > 0) {
        $(this).data("lightbox","image-gallery");
        console.log("test");
    }
});

The console logs, but the data-attribute isn't set. What did I do wrong?


Solution

  • Try

    $(".single .entry-content a:has(> img)").attr('data-lightbox', 'image-gallery')
    

    Demo: Fiddle