]> source.dussan.org Git - jquery.git/commitdiff
Attributes: Trim whitespace from option text when returned as a value
authorJohn Hoven <hovenj@gmail.com>
Thu, 6 Mar 2014 19:56:09 +0000 (13:56 -0600)
committerDave Methvin <dave.methvin@gmail.com>
Thu, 20 Mar 2014 19:44:09 +0000 (15:44 -0400)
Fixes #14858
Ref #14686
Closes gh-1531

AUTHORS.txt
src/attributes/val.js
test/unit/attributes.js

index 70a97e8c3ef8263131dfec9d6f5d89106d0e7d14..0856478f23bbd88dda14b54b3c90ce71a8a3e4e8 100644 (file)
@@ -210,4 +210,5 @@ S. Andrew Sheppard <andrew@wq.io>
 Roman Reiß <me@silverwind.io>
 Benjy Cui <benjytrys@gmail.com>
 Rodrigo Rosenfeld Rosas <rr.rosas@gmail.com>
+John Hoven <hovenj@gmail.com>
 
index b13641b28b19f14199c3f871aa6b0147ab0986a5..4a3b0e5c171fc5c253257cd76e8c9431d7b05174 100644 (file)
@@ -74,7 +74,9 @@ jQuery.extend({
                                var val = jQuery.find.attr( elem, "value" );
                                return val != null ?
                                        val :
-                                       jQuery.text( elem );
+                                       // Support: IE10-11+
+                                       // option.text throws exceptions (#14686, #14858)
+                                       jQuery.trim( jQuery.text( elem ) );
                        }
                },
                select: {
index 32fd7b958e34168fdd74f80bd113ea22f0eee0d1..2ab583caa64deee613c113970f65a3b1245061a1 100644 (file)
@@ -1462,3 +1462,16 @@ test( "should not throw at $(option).val() (#14686)", 1, function() {
                ok( false );
        }
 });
+
+test( "Insignificant white space returned for $(option).val() (#14858)", function() {
+       expect ( 3 );
+
+       var val = jQuery( "<option></option>" ).val();
+       equal( val.length, 0, "Empty option should have no value" );
+
+       val = jQuery( "<option>  </option>" ).val();
+       equal( val.length, 0, "insignificant white-space returned for value" );
+
+       val = jQuery( "<option>  test  </option>" ).val();
+       equal( val.length, 4, "insignificant white-space returned for value" );
+});