Kevin Beal
Kevin Beal

Reputation: 10849

jQuery - How to convert a jQuery object to the kind of object returned w/ getElementById

Occasionally I come across situations where certain I have to use document.getElementById() in order to get certain code to work (like w/ Google Maps API for example) for reasons I don't understand. Is there a jQuery method or some relatively easy way to convert a jQuery object to the sort of object returned w/ document.getElementById()?

Upvotes: 3

Views: 75

Answers (3)

Garett
Garett

Reputation: 16828

Yes you can get the underlying DOM node using

$(myselector)[0]

or

$(myselector).get(0)

In addition to the above:

$(myselector).get(), without an index, will return an array of DOM nodes.

Upvotes: 1

Gabe
Gabe

Reputation: 50493

Something like this:

$("#myId").get(0)

or

$("#myId")[0]

Upvotes: 8

Mitch Dempsey
Mitch Dempsey

Reputation: 39889

You can do $("#someDiv").get(0) which will return the HTMLElement object (which is the same that is returned by calling document.getElementById()

Upvotes: 2

Related Questions