Skip to content

Commit f6302b0

Browse files
committed
Attributes: Use the option val hook in select val hook and simplify it
The hook is still defined; not using it could cause issues in IE<11. Also, IE10 no longer throws when value not set but it still doesn't trim the value. IE11 has all those issues fixed; support comments are updated. Fixes jquerygh-1902 Closes jquerygh-1901
1 parent dd596cc commit f6302b0

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/attributes/val.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,9 @@ jQuery.extend({
7474
valHooks: {
7575
option: {
7676
get: function( elem ) {
77-
var val = jQuery.find.attr( elem, "value" );
78-
return val != null ?
79-
val :
80-
// Support: IE10-11+
81-
// option.text throws exceptions (#14686, #14858)
82-
jQuery.trim( jQuery.text( elem ) );
77+
// Support: IE<11
78+
// option.value not trimmed (#14858)
79+
return jQuery.trim( elem.value );
8380
}
8481
},
8582
select: {
@@ -130,7 +127,8 @@ jQuery.extend({
130127

131128
while ( i-- ) {
132129
option = options[ i ];
133-
if ( (option.selected = jQuery.inArray( option.value, values ) >= 0) ) {
130+
if ( (option.selected =
131+
jQuery.inArray( jQuery.valHooks.option.get( option ), values ) >= 0) ) {
134132
optionSet = true;
135133
}
136134
}

test/unit/attributes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,11 @@ test( "should not throw at $(option).val() (#14686)", 1, function() {
14611461
}
14621462
});
14631463

1464+
test( "option value not trimmed when setting via parent select", function() {
1465+
expect( 1 );
1466+
equal( jQuery( "<select><option> 2</option></select>" ).val( "2" ).val(), "2" );
1467+
});
1468+
14641469
test( "Insignificant white space returned for $(option).val() (#14858)", function() {
14651470
expect ( 3 );
14661471

0 commit comments

Comments
 (0)