Skip to content

Commit 541e734

Browse files
johnhovendmethvin
authored andcommittedMar 20, 2014
Attributes: Trim whitespace from option text when returned as a value
Fixes #14858 Ref #14686 Closes gh-1531 (cherry picked from commit 9ec429c) Conflicts: src/attributes/val.js
1 parent e547a27 commit 541e734

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed
 

‎AUTHORS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,5 @@ S. Andrew Sheppard <andrew@wq.io>
219219
Roman Reiß <me@silverwind.io>
220220
Benjy Cui <benjytrys@gmail.com>
221221
Rodrigo Rosenfeld Rosas <rr.rosas@gmail.com>
222+
John Hoven <hovenj@gmail.com>
222223

‎src/attributes/val.js

+10
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ jQuery.fn.extend({
7171

7272
jQuery.extend({
7373
valHooks: {
74+
option: {
75+
get: function( elem ) {
76+
var val = jQuery.find.attr( elem, "value" );
77+
return val != null ?
78+
val :
79+
// Support: IE10-11+
80+
// option.text throws exceptions (#14686, #14858)
81+
jQuery.trim( jQuery.text( elem ) );
82+
}
83+
},
7484
select: {
7585
get: function( elem ) {
7686
var value, option,

‎test/unit/attributes.js

+13
Original file line numberDiff line numberDiff line change
@@ -1459,3 +1459,16 @@ test( "should not throw at $(option).val() (#14686)", 1, function() {
14591459
ok( false );
14601460
}
14611461
});
1462+
1463+
test( "Insignificant white space returned for $(option).val() (#14858)", function() {
1464+
expect ( 3 );
1465+
1466+
var val = jQuery( "<option></option>" ).val();
1467+
equal( val.length, 0, "Empty option should have no value" );
1468+
1469+
val = jQuery( "<option> </option>" ).val();
1470+
equal( val.length, 0, "insignificant white-space returned for value" );
1471+
1472+
val = jQuery( "<option> test </option>" ).val();
1473+
equal( val.length, 4, "insignificant white-space returned for value" );
1474+
});

0 commit comments

Comments
 (0)