Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing $() jQuery wrapper to just get raw JS element [duplicate]

Random just out of curiosity question:

Let's say for whatever reason I get an element back from a function

$(element)

But I want to remove the $( __ ) jQuery wrapper to leave the regular DOM Element:

element

Is this possible? (I'm sure it'd be smart to test $(element).length() to make sure it isn't more than 1 thing inside beforehand too...

jsFiddle

like image 682
Mark Pieszak - Trilon.io Avatar asked Sep 28 '12 14:09

Mark Pieszak - Trilon.io


People also ask

What does $() return in jQuery?

When a jQuery object is passed to the $() function, a clone of the object is created. This new jQuery object references the same DOM elements as the initial one.

What is $() in JavaScript?

$() The $() function is shorthand for the getElementByID method, which, as noted above, returns the ID of a specific element of an HTML DOM. It's frequently used for manipulating elements in a document. $() allows for shorter and more efficient JavaScript coding.

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?

jQuery also has a method named . get() which provides a related function. Instead of returning a jQuery-wrapped DOM element, it returns the DOM element itself.


1 Answers

var firstElem = $(element)[0];

or

var firstElem = $(element).get(0);

Calling get() without an index gives you an array of the elements.

Reference: jQuery get()

like image 115
epascarello Avatar answered Nov 07 '22 03:11

epascarello