Ticket #9319 (closed bug: fixed)
Default val() getter expects a String returned
| Reported by: | Peter Beverloo | Owned by: | rwaldron |
|---|---|---|---|
| Priority: | low | Milestone: | 1.next |
| Component: | attributes | Version: | 1.6.1 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
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
comment:1 Changed 2 years ago by addyosmani
- Priority changed from undecided to low
- Status changed from new to open
- Component changed from unfiled to manipulation
comment:2 Changed 2 years ago by timmywil
- Component changed from manipulation to attributes
If value should be a number, we should probably leave it as a number. Maybe something like:
ret = elem.value;
return typeof ret === "string" ?
ret.replace(rreturn, "") :
ret == null ?
"" :
ret;
comment:3 Changed 2 years ago by rwaldron
For more information about "meter" and "progress" http://peter.sh/examples/?/html/meter-progress.html
comment:4 Changed 2 years ago by rwaldron
- Owner set to rwaldron
- Status changed from open to assigned
comment:5 Changed 2 years ago by rwaldron
- 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 2 years ago by rwldrn
- Status changed from assigned to closed
- Resolution set to fixed
Landing pull request 382. Adds support for number values (meter,progress); Fixes #9319.
More Details:
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
