Jakub
Jakub

Reputation: 123

jquery attribute + index (in groups)

I have six <div class="gallery-popup"> elements. Inside each there are thumbnails inside <a href="..."> tags. What I want, is to add attribute data-lightbox="gallery-X", to each <a> element, but specific for each one of those 6 galleries. For example gallery-1 for each <a> inside first .gallery-popup, gallery-2 inside second and so on. Code which i have now is :

var thumbnailInterval = setInterval(function () {
    if ($('.offer .box_info_potos div .gallery-popup li a').length) {
        console.log('found' + $('.offer ').length);
        clearInterval(thumbnailInterval);

        $('.offer .gallery-popup a').each(function (index) {
            var a = $(this).get()[0];
            a.setAttribute('data-lightbox', 'gall-' + index);
            console.log(a);
        });
    }
}, 100); 

Upvotes: 0

Views: 52

Answers (2)

Manoj
Manoj

Reputation: 5071

Try this

$('.offer .gallery-popup').each(function (index,item) {
    $(item).find('a').attr('data-lightbox','gall-'+(index+1));
});

See working example

Update

See this image my code doing this.

enter image description here

Upvotes: 1

Bibhudatta Sahoo
Bibhudatta Sahoo

Reputation: 4894

Try like this

 $('.offer .gallery-popup ').find('a').each(function (index,div) {
        div.setAttribute('data-lightbox', 'gall-' + index);
    });

Upvotes: 0

Related Questions