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?
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.
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.
version added: 1.0jQuery( "element" ) Refers to the tagName of DOM nodes.
$() = 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.
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";
The jQuery .get()
will do that for you
http://api.jquery.com/get/
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]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With