How to use jQuery hasClass when the selector is a JS variable

I am trying to use the jQuery hasClass function. It doesn't seem to work when I use it like that. I would appreciate it if anyone could decide how to use hasClass in this situation.

The error I get is

 numValueElement.hasClass is not a function if($(numValueElement.hasClass("tag"))) 

The key line throwing this error is if($(numValueElement.hasClass("tag")))

 $(".numberValue").click ( function () { var numValueElement = this; var propertyId = numValueElement.id; $(".numberBounds").filter("#"+propertyId).toggle(); if($(numValueElement.hasClass("tag"))) { } } ); 
+4
source share
5 answers

.hasClass() is a jQuery object object not associated with the element itself.

You need to know the difference between a wrapped jQuery object and a simple DOM element.

What would you like:

 $(numValueElement).hasClass("class-name") 
+7
source

Like this:

 if( $(yourVariable).hasClass("someClass") ) { } 
+3
source
 if($(numValueElement).hasClass("tag")) 
+3
source

Use it this way $ (NumValueElement) .hasClass ("Tag")

0
source
 if($('#'+this.id).hasClass('class-name')){ } 
0
source

All Articles