Search code examples
jqueryattr

Adding an attribute or class to multiple elements without repeatedly defining the container - jQuery


Been years since I've done any real coding, and I was never great at JS to begin with, so forgive me if this is a dumb question.

How do I add an attribute to multiple elements without having to spell out the container element multiple times? In other words, this works fine:

$('.carousel .content .section.collapsed a[href], .carousel .content .section.collapsed *[tabindex]').attr("inert",""); 

But this doesn't:

$('.carousel .content .section.collapsed')(function() {
    $('a[href], *[tabindex]').attr("inert",""); 
});

I don't want to have to keep adding ".carousel .content .section.collapsed" for every single element in the list.


Solution

  • Use .find() within the container selector:

    $('.carousel .content .section.collapsed').find('a[href], [tabindex]').attr("inert", "");