Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a jQuery object to an Element?

If I have a textarea like var textarea = $('textarea'), how can I set the value to it using the JavaScript property value, and not the jQuery property val()?

I think I need to convert textarea to a JavaScript object first, but how do I?

like image 629
Alex Avatar asked Apr 04 '11 01:04

Alex


People also ask

How do I pull a native DOM element from a jQuery object?

Use the get() method. However, most of the time you want to use jQuery methods to do the manipulation as opposed to getting access to the raw DOM element and then modifying it using "standard" JavaScript. For example, with jQuery you can just say $('mySelector'). addClass('myClass') to add a CSS class to a DOM element.

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 a jQuery element?

version added: 1.0jQuery( "element" ) Refers to the tagName of DOM nodes.

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.


2 Answers

You can use the dereferencing operator, or the .get() method to "Retrieve the DOM elements matched by the jQuery object."

Examples:

txtArea[0].value = "something"; 

or:

txtArea.get(0).value = "something"; 
like image 65
karim79 Avatar answered Oct 09 '22 08:10

karim79


The jQuery .get() will do that for you

http://api.jquery.com/get/

Quoted:

Without a parameter, .get() returns all of the elements:

alert($('li').get()); 

With an index specified, .get() will retrieve a single element:

($('li').get(0)); 

...we can use the array dereferencing operator to get at the list item instead:

alert($('li')[0]); 
like image 26
nonopolarity Avatar answered Oct 09 '22 08:10

nonopolarity