This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 11887 - Remove useless input.valueAsNumber. Pretty much the same functionality can be achieved using parseFloat or parseInt
Summary: Remove useless input.valueAsNumber. Pretty much the same functionality can be...
Status: RESOLVED WONTFIX
Alias: None
Product: HTML WG
Classification: Unclassified
Component: LC1 HTML5 spec (show other bugs)
Version: unspecified
Hardware: Other other
: P3 normal
Target Milestone: ---
Assignee: Ian 'Hixie' Hickson
QA Contact: HTML WG Bugzilla archive list
URL: http://www.whatwg.org/specs/web-apps/...
Whiteboard:
Keywords:
Depends on: 12220
Blocks:
  Show dependency treegraph
 
Reported: 2011-01-27 10:39 UTC by contributor
Modified: 2011-08-04 05:12 UTC (History)
10 users (show)

See Also:


Attachments

Description contributor 2011-01-27 10:39:06 UTC
Specification: http://www.whatwg.org/specs/web-apps/current-work/multipage/common-input-element-attributes.html
Section: http://www.whatwg.org/specs/web-apps/current-work/#common-input-element-apis

Comment:
Remove useless input.valueAsNumber. Pretty much the same functionality can be
actieved using parseFloat or parseInt

Posted from: 80.220.69.171
Comment 1 Joseph Pecoraro 2011-01-27 18:57:41 UTC
input.valueAsNumber is useful for date and range input types. It isn't even allowed on text input types. See the table at:
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-input-element.html#the-input-element
Comment 2 Adrian Bateman [MSFT] 2011-01-30 01:45:23 UTC
Some input type likes type=range store the value internally as a number. Converting to a string for .value and then back to a number in script is a waste. valueAsNumber is useful to avoid this.
Comment 3 Mounir Lamouri 2011-01-30 15:01:21 UTC
(In reply to comment #1)
> input.valueAsNumber is useful for date and range input types. It isn't even
> allowed on text input types. See the table at:
> http://www.whatwg.org/specs/web-apps/current-work/multipage/the-input-element.html#the-input-element

For range and number, |input.valueAsNumber| is equivalent to |parseFloat(input.value)|.
For date related types, |input.valueAsNumber| is equivalent to |input.valueAsDate.milliseconds|.
So, it's not really useful, it's only shorter (in term of characters).
In addition, I think the way .valueAsNumber behaves is really weird: returning the number of milliseconds from the UNIX epoch for date related types isn't intuitive.

(In reply to comment #2)
> Some input type likes type=range store the value internally as a number.
> Converting to a string for .value and then back to a number in script is a
> waste. valueAsNumber is useful to avoid this.

According to the specs: "The input element [in the Range state] represents a control for setting the element's value to a string representing a number.". It's the same for the Number state. And according to the specs you have to parse the value to a floating number when .valueAsNumber is called.
Though, I can understand that implementation could try to optimize .valueAsNumber by storing the value as a number but I don't think it worths keeping this method.
Comment 4 Olli Pettay 2011-01-30 17:16:51 UTC
From API usage point of few the fact that some 
implementation may store the value as number doesn't really matter.
JS developers do know parseInt and parseFloat. Is there any reason why
they should use valueAsNumber?
Comment 5 Mounir Lamouri 2011-02-18 11:56:36 UTC
I've actually found one situation where .valueAsNumber and parseFloat() would differ: if .value begins with a valid number which is followed by a string.
For example: "42 foo"; parseFloat() will return 42 and .valueAsNumber NaN.

That might be a good reason to keep .valueAsNumber I think.
Comment 6 Mounir Lamouri 2011-02-18 12:07:00 UTC
(In reply to comment #5)
> I've actually found one situation where .valueAsNumber and parseFloat() would
> differ: if .value begins with a valid number which is followed by a string.
> For example: "42 foo"; parseFloat() will return 42 and .valueAsNumber NaN.
> 
> That might be a good reason to keep .valueAsNumber I think.

Actually, when valueAsNumber is available, value can't contain something else than a number so my previous comment doesn't apply...
Comment 7 Ian 'Hixie' Hickson 2011-02-28 20:01:43 UTC
valueAsNumber was really just added for completeness after we added valueAsDate, IIRC. I don't mind removing it if people don't want to implement it. As the comments above say, it's purely an authoring convenience, it doesn't provide anything that can't be obtained in another way.
Comment 8 Olli Pettay 2011-02-28 21:04:55 UTC
OK, so let's remove it.
Comment 9 Mounir Lamouri 2011-03-02 17:05:53 UTC
It seems like parsing a floating point number in HTML and in Javascript specifications might differ. I've open bug 12220 about that. I guess we should make sure it's fixed before removing valueAsNumber.
Comment 10 Adrian Bateman [MSFT] 2011-03-02 17:41:44 UTC
In our prototyping we've found the valueAsNumber concept is a useful convenience API that avoids a round-trip through a string. It's not hard to implement and since almost every use case where valueAsNumber is valid involves dealing with the value as a number why not keep it? It makes JavaScript code more readable.
Comment 11 Olli Pettay 2011-03-02 17:45:12 UTC
How makes adding yet another method to DOM/JS the code more readable?
And actually, because of bug 12220, valueAsNumber works currently
rather strangely.
Comment 12 Adrian Bateman [MSFT] 2011-03-02 17:46:57 UTC
It's more readable because it's just a property access with a name that indicates what is happening.
Comment 13 Ian 'Hixie' Hickson 2011-05-05 21:23:07 UTC
Right now Opera and WebKit support it, and IE and Firefox don't, but Microsoft is arguing in favour. This to me seems to suggest we should keep it, even though it's very much a convenience method and doesn't really provide anything truly novel. I'm not closing this bug yet, though; if the people who want for us to drop this can convince the vendors who have shipped with it to remove it then I'd be happy to remove it.
Comment 14 Ian 'Hixie' Hickson 2011-05-09 21:49:03 UTC
Came across an interesting example of the convenience for this recently: changing the value of a range control by a certain known delta:

   input.valueAsNumber += delta;

The alternatives are a bit of a pain:

   input.value = parseFloat(input.value) + delta;

   if (delta > 0)
     input.stepUp(delta/parseFloat(input.step));
   else
     input.stepDown(delta/parseFloat(input.step));
Comment 15 Ian 'Hixie' Hickson 2011-08-03 07:21:14 UTC
EDITOR'S RESPONSE: This is an Editor's Response to your comment. If you are satisfied with this response, please change the state of this bug to CLOSED. If you have additional information and would like the editor to reconsider, please reopen this bug. If you would like to escalate the issue to the full HTML Working Group, please add the TrackerRequest keyword to this bug, and suggest title and text for the tracker issue; or you may create a tracker issue yourself, if you are able to do so. For more details, see this document:
   http://dev.w3.org/html5/decision-policy/decision-policy.html

Status: Rejected
Change Description: no spec change
Rationale: see last two comments
Comment 16 Michael[tm] Smith 2011-08-04 05:12:35 UTC
mass-move component to LC1