From d5e6e58902b0102328e8d1e097978380b290cdff Mon Sep 17 00:00:00 2001
From: Anton Rieder The For selects and checkboxes, you can also use the :selected and :checked selectors to get at values, for example: jQuery 3 changes the behavior of this method to return an empty array if a collection is a .val()
method is primarily used to get the values of form elements such as input
, select
and textarea
. When the first element in the collection is a select-multiple
(i.e., a select
element with the multiple
attribute set), it returns an array containing the value of each selected option, or null
if no options are selected. When called on an empty collection, it returns undefined
.select-multiple
and no options are selected.
// Get the value from a dropdown select
$( "select.foo option:selected").val();
@@ -43,6 +44,8 @@ $.valHooks.textarea = {
function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "#multiple" ).val() || [];
+ // When using jQuery 3:
+ // var multipleValues = $( "#multiple" ).val()
$( "p" ).html( "Single: " + singleValues +
" Multiple: " + multipleValues.join( ", " ) );
}