Skip to content

Commit 56358d0

Browse files
aried3rkswedberg
authored andcommitted
Fix documentation for .val()
1 parent 1b230db commit 56358d0

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

entries/val.xml

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<p>The <code>.val()</code> method is primarily used to get the values of form elements such as <code>input</code>, <code>select</code> and <code>textarea</code>. When called on an empty collection, it returns <code>undefined</code>.</p>
1515
<p>When the first element in the collection is a <code>select-multiple</code> (i.e., a <code>select</code> element with the <code>multiple</code> attribute set), <code>.val()</code> returns an array containing the value of each selected option. <strong>As of jQuery 3.0</strong>, if no options are selected, it returns an empty array; <strong>prior to jQuery 3.0</strong>, it returns <code>null</code>.</p>
1616
<p>For selects and checkboxes, you can also use the <a href="/selected-selector/">:selected</a> and <a href="/checked-selector/">:checked</a> selectors to get at values, for example:</p>
17+
<p><strong>jQuery 3</strong> changes the behavior of this method to return an empty array if a collection is a <code>select-multiple</code> and no options are selected.</p>
1718
<pre><code>
1819
// Get the value from a dropdown select
1920
$( "select.foo option:selected").val();
@@ -44,6 +45,8 @@ $.valHooks.textarea = {
4445
function displayVals() {
4546
var singleValues = $( "#single" ).val();
4647
var multipleValues = $( "#multiple" ).val() || [];
48+
// When using jQuery 3:
49+
// var multipleValues = $( "#multiple" ).val()
4750
$( "p" ).html( "<b>Single:</b> " + singleValues +
4851
" <b>Multiple:</b> " + multipleValues.join( ", " ) );
4952
}

0 commit comments

Comments
 (0)