Alex Rivera | Logout

Getting the base element from a jQuery object

Asked 2008-09-06T21:10:33.453
69

I'm struggling to find the right terminology here, but if you have jQuery object...

$('#MyObject')

...is it possible to extract the base element? Meaning, the equivalent of this:

document.getElementById('MyObject')
Edit
Report

2 Answers

56

Yes, use .get(index). According to the documentation:

The .get() method grants access to the DOM nodes underlying each jQuery object.

answered 2008-09-06T21:15:25.857
5

I tested Aaron's statements on all the browsers I have available on my box:

$('#MyObject').get(0);

vs

$('#MyObject')[0];

As far as I can tell, it is only a matter of personal preference.

Functionally, both these statements are equivalent for both existing and non-existing elements. I tested the following browsers: Chrome 27.0, FF 21.0, IE10, IE9, IE8, IE7, IE6.

In the speed tests that I ran, it was not always possible to tell which variation was faster; the outcome was not always consistent, even on the same browser. For the speed tests, I only tested existing elements. My test results are here.

answered 2013-05-30T15:06:37.127

Your Answer