5

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")))
            {
            }

        }
    );

5 Answers 5

9

.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")
Sign up to request clarification or add additional context in comments.

Comments

3

Like this:

if( $(yourVariable).hasClass("someClass") ) {

}

Comments

3
if($(numValueElement).hasClass("tag"))

Comments

0

Use it in this way $(numValueElement).hasClass("tag")

Comments

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

}

1 Comment

Please add some explanation of why this code helps the OP. This will help provide an answer future viewers can learn from. See How to Answer for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.