0

I have a problem that i cannot get a class from a variable in javascript.

var li_id = LI_element.id;
if(!($("'#"+li_id+"'").hasClass("abc"))) {
    console.log(LI_element);
}

Here, li_id gives me id of an element and i want to check that this element has the class abc or not. But, when i execute the above command it gives me error that says

throw new Error( "Syntax error, unrecognized expression: " + msg );

I assume there is some error in the if statement. Thanks for your help!

2
  • 1
    What is the msg of the error? Also you can simply use $(LI_element).hasClass(... instead of concatenating a selector together. Commented Oct 11, 2013 at 10:47
  • Why you just don't use LI_element to check if hasClass and need to convert a id variable and check if that #id hasClass? Commented Oct 11, 2013 at 10:50

1 Answer 1

4

In the selector you have extra quotes ', which shouldn't be there.

Otherwise just use the following syntax:

if (!$(LI_element).hasClass("abc")) {
    console.log(LI_element);
}
Sign up to request clarification or add additional context in comments.

Comments

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.