.val()Returns: String, Number, Array
Description: Get the current value of the first element in the set of matched elements.
-
version added: 1.0.val()
- This method does not accept any arguments.
The .val()
method is primarily used to get the values of form elements such as input
, select
and textarea
. In the case of <select multiple="multiple">
elements, the .val()
method returns an array containing each selected option; if no option is selected, it returns null
.
For selects and checkboxes, you can also use the :selected and :checked selectors to get at values, for example:
1
2
3
4
|
|
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:
1
2
3
4
5
|
|
Examples:
Example: Get the single value from a single select and an array of values from a multiple select and display their values.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
|
Demo:
Example: Find the value of an input box.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
|