Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  20] [ 2]  / answers: 1 / hits: 40150  / 13 Years ago, thu, july 28, 2011, 12:00:00

How do I determine if the .attr('class') exists?



I am using this code:



if(jQuery(this).attr('class') != undefined && jQuery(this).hasClass('myclass')) {
//Do something
}


It doesn't seem to work.


More From » jquery

 Answers
185

It depends on what you want. Do you want to check if an element has the class attribute, such as:



<div class></div>
<div class=></div>
<div class=abc></div>


If so, use:



if ($('div').attr('class') != undefined)...


If you're trying to check if the element has a specific class, use:



if ($('div').hasClass('abc'))...

[#90947] Wednesday, July 27, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kylee

Total Points: 60
Total Questions: 119
Total Answers: 101

Location: Bonaire
Member since Wed, Mar 29, 2023
1 Year ago
;