Reputation: 5547
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
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