jQuery: How to check if two elements have the same class

11,135

Solution 1

I'd suggest:

$('a').each(
    function() {
        var classes = this.classList;
        for (var i=0,len=classes.length; i<len; i++){
            if ($('body').hasClass(classes[i])){
                $(this).addClass('bodySharesClass');
            }
        }
    });​

JS Fiddle demo.


Edited to try and account for Internet Explorer's inability to understand/use/implement .classList:

if (!document.createElement().classList){
    var useAttr = true;
}

$('a').each(
    function(i) {
        var classes = [];
        if (!useAttr){            
            classes = this.classList;
        }
        else {
            classes = $(this).attr('class');
        }
        for (var i=0,len=classes.length; i<len; i++){
            if ($('body').hasClass(classes[i])){
                $(this).addClass('bodySharesClass');
            }
        }
    });

JS Fiddle demo.


Edited because it appears my previous attempt, to account for IE, failed. Sigh. Still! This approach should work, albeit I can't test it, as I'm without both IE and Windows:

$('a').each(
    function(i) {
        var classes = this.className.split(/\s+/);
        for (var i=0,len=classes.length; i<len; i++){
            if ($('body').hasClass(classes[i])){
                $(this).addClass('bodySharesClass');
            }
        }
    });

JS Fiddle demo.

References:

Solution 2

ok, assuming I understand you correctly, you can do something like this...

var link = $(".foo");//or select your link another way
var linkClass = link.attr("class");
if($("body").hasClass(linkClass)){
   //link has matching class
   link.addClass("newClass");
}

As you can see I have used the hasClass() JQuery function to check if the body tab has the matching class.


If your link will potentially have more than one class name you can do it like this...

var link = $(".foo");//or select your link another way
var linkClass = link.attr("class");
var classList = linkClass.split(/\s+/);

var matchFound = false;

for(var i = 0; i < classList.length; i++){
    if($("body").hasClass(classList[i])){
       //link has matching class
       matchFound = true;
    }
}

if(matchFound){
    link.addClass("newClass");
}

Also, if you want to process all your links at the same time you could wrap it all in a JQuery each() and change the first line like so...

$("a").each(function(index){
   var link = $(this);

   //the rest of the above code here
});

Solution 3

$('a').hasClass('foo').addClass('whatever');
Share:
11,135
Dustin
Author by

Dustin

Updated on June 28, 2022

Comments

  • Dustin
    Dustin almost 2 years

    Is there a way to check if two elements have the same class name? My code looks like this...

    <body class="foo bar cats">
    <a href="#" class="foo">Link</a>
    <a href="#" class="puppies">Link</a>
    

    What I want to do is add another class to the foo link if it matches a class in the body. How would I check if one of the classes in my links matches a class in the body with jQuery?

  • xdazz
    xdazz over 12 years
    What about the link has multiple classes?
  • Piskvor left the building
    Piskvor left the building over 12 years
    @xdazz: Apparently you didn't read this part of the linked documentation: "The .hasClass() method will return true if the class is assigned to an element, even if other classes also are."
  • musefan
    musefan over 12 years
    @xdazz: The it becomes a different question. html/javascript can change so much it is pointless trying to cater for "what if's". Always better to code to your current requirements and keep the javascript as clean as possible
  • xdazz
    xdazz over 12 years
    @Piskvor You didn't understand my mean. I mean if link has multiple classes, link.attr("class") will return something like foo foo1 foo2, so does .hasClass() work?
  • Piskvor left the building
    Piskvor left the building over 12 years
    @xdazz: I probably didn't. link.hasClass("foo1") would still return true in your example (which is what the documentation says, as quoted above). Is that what you're asking?
  • Dustin
    Dustin over 12 years
    Thanks for the update to fix IE. It was broke in Firefox too.