I am trying to use jQuery's hasClass
function. It doesn't seem to work when I use it like this. I would appreciate it if anyone can work out how to use hasClass in this situation.
The error I am getting is
numValueElement.hasClass is not a function
if($(numValueElement.hasClass("tag")))
The key line throwing this error is this one if($(numValueElement.hasClass("tag")))
$(".numberValue").click
(
function ()
{
var numValueElement = this;
var propertyId = numValueElement.id;
$(".numberBounds").filter("#"+propertyId).toggle();
if($(numValueElement.hasClass("tag")))
{
}
}
);
jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".
To check if an element contains a class, you use the contains() method of the classList property of the element:*
The jQuery hasClass() method is used to check whether selected elements have specified class name or not. It returns TRUE if the specified class is present in any of the selected elements otherwise it returns FALSE. Syntax: $(selector).
In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.
.hasClass()
is a jQuery object method, not something attached to the element itself.
You need to know the difference between a jQuery object wrapped element, and a plain DOM element.
What you want is:
$(numValueElement).hasClass("class-name")
Like this:
if( $(yourVariable).hasClass("someClass") ) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With