Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  170] [ 1]  / answers: 1 / hits: 16094  / 11 Years ago, mon, may 6, 2013, 12:00:00

How can I get a #<HTMLDivElement> as a jQuery object?



I need to do the following: I have a list of div's with the class contents. So I iterate over it until I find the one with the additional class: test



here is my code:



$.each( $(.contents), function( key, value ) {
if (value.hasClass(test))
{
alert(got it);
}
});


I'm getting the exception: Uncaught TypeError: Object #<HTMLDivElement> has no method 'hasClass'


More From » jquery

 Answers
18

The each() function gives you DOM object and you have to convert it to jQuery object. You can pass value to $ jQuery function to convert it to jQuery object.



$.each( $(.contents), function( key, value ) {
if ($(value).hasClass(test))
{
alert(got it);
}
});


You do not need to iterate through each and simplify it like



elements = $(.contents.text)

[#78411] Friday, May 3, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shaynelandenb

Total Points: 293
Total Questions: 97
Total Answers: 94

Location: Monaco
Member since Fri, Sep 24, 2021
3 Years ago
;