Julien Laroche Julien Laroche - 1 year ago 166
HTML Question

Automaticaly add data-fancybox attribute to img wrapper using jquery

I'd like to add

data-fancybox="gallery"
to all my
<a>
wrapper.

I have this code, it actually wrap all
<img>
with an
<a>
and get the src.

$("img").each(function () {
$(this).wrap($('<a/>', {
href: $(this).attr('src'),
selector : '[data-fancybox="images"]',
}));
});


I would like to replace the
selector : '[data-fancybox="images"]',
by
data-fancybox="gallery"
... I can't find out how to do this.

Thanks for your help folks!

Answer Source

You can use .attr(key, value) method to set attribute value

$("img").each(function () {
    $(this).wrap($('<a/>', {
            href: $(this).attr('src')
        }).attr('data-fancybox', 'gallery'));
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download