Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the original element from the jQuery wrapper

Assuming I have this:

var wrap = $("#someId");

I need to access the original object that I would get by

var orig = document.getElementById("someId");

But I don't want to do a document.getElementById.

Is there something I can use on wrap to get it? something like:

var orig = wrap.original();

I searched high and low but I didn't find anything; or maybe I'm not looking for the right thing.

like image 951
brb Avatar asked Jul 08 '11 10:07

brb


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.

How do I get DOM element?

The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID with the getElementById() method of the document object. In the Console, get the element and assign it to the demoId variable. Logging demoId to the console will return our entire HTML element.

How do you wrap an element in JavaScript?

querySelector('selector'); // wrapping the event form in a row divWrapper = document. createElement('div'); divWrapper. className = 'row'; wrap_single(elementToWrap, divWrapper);


2 Answers

The function for this is get. You can pass an index to get to access the element at that index, so wrap.get(0) gets the first element (note that the index is 0-based, like an array). You can also use a negative index, so wrap.get(-2) gets the last-but-one element in the selection.

wrap.get(0);  // get the first element
wrap.get(1);  // get the second element
wrap.get(6);  // get the seventh element
wrap.get(-1); // get the last element
wrap.get(-4); // get the element four from the end

You can also use array-like syntax to access elements, e.g. wrap[0]. However, you can only use positive indexes for this.

wrap[0];      // get the first element
wrap[1];      // get the second element
wrap[6];      // get the seventh element
like image 150
lonesomeday Avatar answered Oct 11 '22 16:10

lonesomeday


$("#someId") will return a jQuery object, so to get at the actual HTML element you can do:

wrap[0] or wrap.get(0).

like image 5
Jack Franklin Avatar answered Oct 11 '22 15:10

Jack Franklin