Kyle
Kyle

Reputation: 5547

How to check if a table row has a class name without using hasClass()?

I need to check whether a table row (TR) has a class by name. So far, I have the following:

var myClass = "myClass";

//only myClass if it doesn't exist already
if (!(rowGet.className == "myClass") || !(rowGet.className == "myClass anotherClass")) {
     if (rowGet) { // only add the class if TR exists
         rowGet.className = myClass;
     }
}

rowGet is a TR from a table. When I tried hasClass(myClass), I get an error saying HTMLTableElement has no method hasClass. Yes, I have jQuery referenced.

Upvotes: 2

Views: 5517

Answers (2)

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94319

Try

$(rowGet).hasClass("myClass");

Upvotes: 2

Joseph Silber
Joseph Silber

Reputation: 219940

You have to wrap your element with jQuery:

$(rowGet).hasClass('myclass');

Actually, there's no need to first check whether it already has the class applied, just use addClass:

$(rowGet).addClass('myclass');

It won't even complain if the element doesn't exist.

Upvotes: 5

Related Questions