Description: Get the current value of the first element in the set of matched elements.
The .val()
method is primarily used to get the values of form elements such as input
, select
and textarea
. When called on an empty collection, it returns undefined
.
When the first element in the collection is a select-multiple
(i.e., a select
element with the multiple
attribute set), .val()
returns an array containing the value of each selected option. As of jQuery 3.0, if no options are selected, it returns an empty array; prior to jQuery 3.0, it returns null
.
For selects, checkboxes and radio buttons, you can use :checked to select the right elements. For example:
1 2 3 4 5 6 7 8 9 10 11 |
|
Note: At present, using .val()
on <textarea>
elements strips carriage return characters from the browser-reported value. When this value is sent to the server via XHR, however, carriage returns are preserved (or added by browsers which do not include them in the raw value). A workaround for this issue can be achieved using a valHook as follows: