#9319 closed bug (fixed)
Default val() getter expects a String returned
Reported by: | Peter Beverloo | Owned by: | Rick Waldron |
---|---|---|---|
Priority: | low | Milestone: | 1.next |
Component: | attributes | Version: | 1.6.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
Code (https://github.com/jquery/jquery/blob/master/src/attributes.js#L168):
return (elem.value || "").replace(rreturn, "");
This code assumes that elem.value will return a String. This is true for most form-related elements, but <meter> and <progress> (both of which have @value and are form-associated) define their value-property as a double. Doubles do not have a replace method, resulting in a JavaScript error.
Reproduction code:
var meterElement = document.createElement ('meter'), jqMeter = $ (meterElement); meterElement.value = .5; jqMeter.val ();
Tested in Google Chrome 13 with jQuery 1.6.1.
Two proposed fixes:
1) Force elem.value to be a string:
return ((elem.value || "") + "").replace(rreturn, "");
2) Add valHooks for both <meter> as <progress> doing the cast:
jQuery.each([ "meter", "progress" ], function() { jQuery.valHooks[ this ] = { get: function( elem ) { // The meter and progress elements define .value as a double return elem.value + ""; } }; });
The issue was identified by Wilfred Nas, http://twitter.com/#!/wnas/status/70527764922499074
Change History (9)
comment:1 Changed 12 years ago by
Component: | unfiled → manipulation |
---|---|
Priority: | undecided → low |
Status: | new → open |
comment:2 Changed 12 years ago by
Component: | manipulation → attributes |
---|
comment:3 Changed 12 years ago by
For more information about "meter" and "progress" http://peter.sh/examples/?/html/meter-progress.html
comment:4 Changed 12 years ago by
Owner: | set to Rick Waldron |
---|---|
Status: | open → assigned |
comment:5 Changed 12 years ago by
Keywords: | needsdocs added |
---|
After discussing this, timmywil and I agree that returning a number is the right thing to do and that type "number" should be added to the list of returns.
comment:7 Changed 12 years ago by
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Landing pull request 382. Adds support for number values (meter,progress); Fixes #9319.
More Details:
comment:8 Changed 12 years ago by
I added "Number" to the docs entry:
entry type='method' name="val" return="String, Number, Array"
Someone with a better handle on the docs might want to add an example?
comment:9 Changed 12 years ago by
Keywords: | needsdocs removed |
---|
If value should be a number, we should probably leave it as a number. Maybe something like: