.val()Returns: String or Number or Array
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 also use the :selected and :checked selectors to get at values. For example:
1
2
3
4
5
6
7
8
9
10
11
12
|
|
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:
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
41
42
43
44
45
46
47
|
|
Demo:
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
22
23
24
25
26
27
28
29
|
|
Demo:
.val( value )Returns: jQuery
Description: Set the value of each element in the set of matched elements.
-
version added: 1.0.val( value )
-
valueA string of text, a number, or an array of strings corresponding to the value of each matched element to set as selected/checked.
-
-
version added: 1.4.val( function )
-
functionA function returning the value to set.
this
is the current element. Receives the index position of the element in the set and the old value as arguments.
-
This method is typically used to set the values of form fields.
val()
allows you to pass an array of element values. This is useful when working on a jQuery object containing elements like <input type="checkbox">
, <input type="radio">
, and <option>
s inside of a <select>
. In this case, the input
s and the option
s having a value
that matches one of the elements of the array will be checked or selected while those having a value
that doesn't match one of the elements of the array will be unchecked or unselected, depending on the type. In the case of <input type="radio">
s that are part of a radio group and <select>
s, any previously selected element will be deselected.
Setting values using this method (or using the native value
property) does not cause the dispatch of the change
event. For this reason, the relevant event handlers will not be executed. If you want to execute them, you should call .trigger( "change" )
after setting the value.
The .val()
method allows setting the value by passing in a function. As of jQuery 1.4, the function is passed two arguments, the current element's index and its current value:
1
2
3
|
|
This example appends the string " items" to the text inputs' values.
Examples:
Set 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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
|
Demo:
Use the function argument to modify 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
22
|
|
Demo:
Set a single select, a multiple select, checkboxes and a radio button .
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
|
|