From ae817b233f250d8c107b44982c3ae641e3d26222 Mon Sep 17 00:00:00 2001
From: Richard Gibson The When the first element in the collection is a For selects, checkboxes and radio buttons, you can also use the :selected and :checked selectors to get at values. For example: For selects, checkboxes and radio buttons, you can also use the :checked selector to get at values. For example: Note: At present, using Setting values using this method (or using the native The This example appends the string " items" to the text inputs' values. This example removes leading and trailing whitespace from the text inputs' values. The When the first element in the collection is a For selects, checkboxes and radio buttons, you can also use the :checked selector to get at values. For example: For selects, checkboxes and radio buttons, you can use :checked to select the right elements. For example: Note: At present, using This example removes leading and trailing whitespace from the text inputs' values. This example removes leading and trailing whitespace from the values of text inputs with a "tags" class. This example removes leading and trailing whitespace from the values of text inputs with a "tags" class. This example removes leading and trailing whitespace from the values of text inputs with a "tags" class..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
.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
.
// Get the value from a dropdown select
-$( "select.foo option:selected").val();
+$( "select.foo option:checked").val();
// Get the value from a dropdown select even easier
$( "select.foo" ).val();
// Get the value from a checked checkbox
-$( "input:checkbox:checked" ).val();
+$( "input[type=checkbox]:checked" ).val();
// Get the value from a set of radio buttons
-$( "input:radio[name=bar]:checked" ).val();
+$( "input[type=radio][name=bar]:checked" ).val();
.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: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..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:
-
-$( "input:text.items" ).val(function( index, value ) {
- return value + " " + this.className;
+$( "input[type=text].tags" ).val(function( index, value ) {
+ return value.trim();
});
.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
.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
.
+// Get the value from the selected option in a dropdown
+$( "select#foo option:checked" ).val();
-// Get the value from a dropdown select
-$( "select.foo option:checked").val();
-
-// Get the value from a dropdown select even easier
-$( "select.foo" ).val();
+// Get the value from a dropdown select directly
+$( "select#foo" ).val();
// Get the value from a checked checkbox
-$( "input[type=checkbox]:checked" ).val();
+$( "input[type=checkbox][name=bar]:checked" ).val();
// Get the value from a set of radio buttons
-$( "input[type=radio][name=bar]:checked" ).val();
+$( "input[type=radio][name=baz]:checked" ).val();
.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: