This code is not working:
$("div").hasClass("testSection").attr("id");
I get the following error message:
TypeError: $(…).hasClass(…).attr is not a function
Any idea what the problem could be?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The issue is because
hasClass()returns a boolean, not a jQuery object. Hence callingattr()on a boolean results in the error you’re seeing.Assuming there is only one div with the specified class you can use the class in the selector:
If there are multiple divs with the class, you’d need to loop: