Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the base element from a jQuery object

Tags:

jquery

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') 
like image 624
dansays Avatar asked Sep 06 '08 21:09

dansays


People also ask

Is it possible to access the underlying DOM element using jQuery?

The Document Object Model (DOM) elements are something like a DIV, HTML, BODY element on the HTML page. A jQuery Selector is used to select one or more HTML elements using jQuery. Mostly we use Selectors for accessing the DOM elements.

Which methods return the element as a jQuery object?

The jQuery selector finds particular DOM element(s) and wraps them with jQuery object. For example, document. getElementById() in the JavaScript will return DOM object whereas $('#id') will return jQuery object.

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.

How do I select a specific DOM node in jQuery?

If you have a variable containing a DOM element, and want to select elements related to that DOM element, simply wrap it in a jQuery object. var myDomElement = document. getElementById( "foo" ); // A plain DOM element. $( myDomElement ).


2 Answers

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

I think that's what you want. I think you can also reference it like a regular array with:

$('#MyObject')[0]; 

But I'm not sure if that will always work. Stick with the first syntax.

like image 54
Aaron Wagner Avatar answered Sep 19 '22 13:09

Aaron Wagner


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

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

like image 21
VolkerK Avatar answered Sep 21 '22 13:09

VolkerK