Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object #<HTMLDivElement> as jQuery object

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'

like image 541
gurehbgui Avatar asked Dec 15 '22 11:12

gurehbgui


1 Answers

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")
like image 76
Adil Avatar answered Jan 08 '23 17:01

Adil