Variables are not typed; their values are. The conversion between a
string and a number happens automatically. Since plus (`+`) is
also used as in string concatenation, `'1' + 1` is equal to `'11'`.
The string determines what `+` does. To overcome this, first convert the
string to a number. For example: `+varname` or `Number(varname)` or
`parseInt(varname, 10)` or `parseFloat(varname)`.
Form control values are strings, as is the result from a `prompt`
dialog. Convert these to numbers before performing addition by using
the unary `+` operator: `+'1' + 1` result is `2`.
Additional Notes: <URL: http://jibbering.com/faq/notes/type-conversion/>
<URL: http://msdn.microsoft.com/en-us/library/67defydd%28VS.85%29.aspx>
The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/
--
The sendings of these daily posts are proficiently hosted
by http://www.pair.com.
>FAQ Topic - Why does 1+1 equal 11? or How do I convert a
>string to a number?
>Form control values are strings,
That is true even for checkboxes, it seems.
In my IE & FF & Opera, checkbox.value is the string 'on'.
In my Safari and Chrome, it is 'on' if checked, else ''.
In all, I expect checkbox.checked is a boolean.
--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 7.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.
Why wouldn't it be?
>
> In my IE & FF & Opera, checkbox.value is the string 'on'.
Yes, that's the default value.
>
> In my Safari and Chrome, it is 'on' if checked, else ''.
That's a bit of a wacky implementation, but shouldn't cause any
trouble. Serialization functions skip checkboxes if they are
unchecked, so the reported value is irrelevant.
>
> In all, I expect checkbox.checked is a boolean.
It is.
All html elements' attributes are always strings, ISTM.
--
Jorge.
But that's not an attribute (it's a DOM property). Why does it seem
the whole world is confused about this?
The only (direct) way to get an attribute value is with the
getAttribute method. It returns a string or null (the latter when the
attribute is not present). IE's implementation of this method in
versions prior to 8 (and Compatibility View in 8) is broken as
designed (it returns property values).
I said "All *html*elements'*attributes* are always strings, ISTM.
--
Jorge.
That's not true. Many/most/all? -standard- attributes are (directly)
reflected in JSLand as properties, e.g. document.body.style.marginTop
or document.body.bgColor. It might not be so in the most borken
browser ever... (?)
--
Jorge.
Go back and read the part you snipped and you will see why I corrected
your mistake. It doesn't matter if you knew what you were saying.
The context in which you said it was wrong, making it look like there
was confusion about the issue of the checked property of a checkbox.
And attributes aren't anything. They are an abstract concept. You
can retrieve attribute values as I indicated and, if present, they
will be returned in string form (in most browsers).
It should return whatever string value you have assigned to the
element's value attribute. The value attribute is required in HTML
4.01 for radio buttons and checkboxes.
<URL: http://www.w3.org/TR/html401/interact/forms.html#adef-value-INPUT
>
I'll take a punt and say you didn't assign a value and are getting
whatever the browser decides to assign to invalid markup. Play with
the following:
<div>
<input type="radio" name="rad0" onclick="
alert(
'value property: ' + this.value +
'\nvalue attribute: ' + this.getAttribute('value') +
'\ntypeof checked property: ' +
(typeof this.checked) + ' ' + this.checked +
'\ntypeof checked attribute: ' +
(typeof this.getAttribute('checked')) + ' ' +
this.getAttribute('checked')
);
">
</div>
HTML 5 is vague about it:
"Form controls have a value and a checkedness. (The latter is only
used by input elements.) These are used to describe how the user
interacts with the control."
<URL: http://www.w3.org/TR/html5/association-of-controls-and-forms.html#concept-fe-value
>
The word "checkedness" is awkward and confusing, what's wrong with
"checkedstate"? I don't think value has anything to do with how a user
interacts with a control.
> In all, I expect checkbox.checked is a boolean.
The checked property is specified in DOM 2 HTML as being boolean. Any
other type is non-conforming.
<URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-30233917 >
--
Rob
The type of the checked *attribute* is boolean, but its value as
reflected in JSLand is *not* of boolean type.
--
Jorge.