aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Hoven <hovenj@gmail.com>2014-03-06 13:56:09 -0600
committerDave Methvin <dave.methvin@gmail.com>2014-03-20 15:47:35 -0400
commit541e7349b6533eb533c15d17e3e9e432e4a719ea (patch)
treec6fdbc92c1fac7fd8f4c1ce2eb93c30ce336b67a
parente547a2775fc79e4b2eb807da9d30719afc570458 (diff)
downloadjquery-541e7349b6533eb533c15d17e3e9e432e4a719ea.tar.gz
jquery-541e7349b6533eb533c15d17e3e9e432e4a719ea.zip
Attributes: Trim whitespace from option text when returned as a value
Fixes #14858 Ref #14686 Closes gh-1531 (cherry picked from commit 9ec429cf6270e455aba4eba85f4db80e633806b6) Conflicts: src/attributes/val.js
-rw-r--r--AUTHORS.txt1
-rw-r--r--src/attributes/val.js10
-rw-r--r--test/unit/attributes.js13
3 files changed, 24 insertions, 0 deletions
diff --git a/AUTHORS.txt b/AUTHORS.txt
index 5a4b804a8..f68f3304a 100644
--- a/AUTHORS.txt
+++ b/AUTHORS.txt
@@ -219,4 +219,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>
diff --git a/src/attributes/val.js b/src/attributes/val.js
index 6d6b8ea7c..42ef32343 100644
--- a/src/attributes/val.js
+++ b/src/attributes/val.js
@@ -71,6 +71,16 @@ jQuery.fn.extend({
jQuery.extend({
valHooks: {
+ option: {
+ get: function( elem ) {
+ var val = jQuery.find.attr( elem, "value" );
+ return val != null ?
+ val :
+ // Support: IE10-11+
+ // option.text throws exceptions (#14686, #14858)
+ jQuery.trim( jQuery.text( elem ) );
+ }
+ },
select: {
get: function( elem ) {
var value, option,
diff --git a/test/unit/attributes.js b/test/unit/attributes.js
index 4a9bb677e..7839abd85 100644
--- a/test/unit/attributes.js
+++ b/test/unit/attributes.js
@@ -1459,3 +1459,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" );
+});