Concatenation instead of adition!

J

Jeremy

On an HTML Page I have:

<input type="hidden" name="p_row" value="1" />

then in script we have:

document.forms.grid.p_row.value=document.forms.grid.p_row.vaue+10;

Why does this give me 110 as the result? I can see it's concatenating
the string values but how do I tell it to add the things together?
 
A

ASM

Jeremy a écrit :
On an HTML Page I have:

<input type="hidden" name="p_row" value="1" />

then in script we have:

document.forms.grid.p_row.value=document.forms.grid.p_row.vaue+10;

Why does this give me 110 as the result?

values in inputs are strings

conversion in a number

string * 1
or
+string
and
probably
Number(string)

= +10 + document.forms.grid.p_row.value;

or

= document.forms.grid.p_row.value*1 + 10;
 
M

Michael Winter

Jeremy wrote:

[snip]
So I can folow Martin Honnen's suggestion and do this:

document.forms.grid.p_row.value=document.forms.grid.p_row.value*1+10;

Martin didn't suggest that, exactly. The FAQ entry he pointed you to
describes no less than six means of performing string to number type
conversion.
or yours and do this:

document.forms.grid.p_row.value=+document.forms.grid.p_row.value+10;

One of those methods was unary plus.
Both appear to work equally well - does it matter which - is one a
"better" method and if so, why?

If you'd followed the link to the FAQ notes, you'd have a partial answer
to that. The unary plus operator is the fastest, though it sometimes
better to wrap the expression in parentheses:

(+string)

For explicitness, the Number constructor function called as a function
is another good alternative:

Number(string)

I wouldn't recommend operator trickery like multiplying by 1 or
subtracting zero as neither of these are primarily designed to perform
type conversion, whereas the other two approaches above are.

I recommend that you read the type conversion article[1] in the FAQ notes.

On a different note, there's little need to keep referencing form
controls from the document object. See "Efficient use of Form
Accessors"[2], part of another FAQ notes article.

Mike


[1] <http://www.jibbering.com/faq/faq_notes/type_convert.html>
[2] <http://www.jibbering.com/faq/faq_notes/form_access.html#faEff>
 
R

Richard Cornford

Jeremy said:
I stand corrected - I took the first solution I saw :) and stopped
reading there.

I suppose that is quicker than trying to understand what you are doing.

Richard.
 
J

Jeremy

I suppose that is quicker than trying to understand what you are doing.

<ouch>

Well I understood enough that we had to tell javascript that these two
variables had to be treated as numbers. The first method satisfied this
need.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue,
26 Sep 2006 17:26:24 remote, seen in ASM
= +10 + document.forms.grid.p_row.value;

Concatenates. Try

= 10 + +document.forms.grid.p_row.value
= +document.forms.grid.p_row.value + 10

N.B. + + != ++ .
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Tue, 26 Sep 2006 15:59:34 remote, seen in
Jeremy said:
document.forms.grid.p_row.value=document.forms.grid.p_row.vaue+10;

Why does this give me 110 as the result?
FAQ.

I can see it's concatenating
the string values but how do I tell it to add the things together?

document.forms.grid.p_row.value -= -10
// & document.forms.grid.p_row.value -=- 10 // :-(

work for me.

It's a good idea to read the newsgroup and its FAQ. See below.
 
R

Richard Cornford

Dr John Stockton wrote:
document.forms.grid.p_row.value -= -10
// & document.forms.grid.p_row.value -=- 10 // :-(

work for me.

But what is your point? That the second should not work? It may not be a
particularly clear formatting of source code for humans but it is
unambiguous to tokenise, so the interpreter will not have a problem
seeing the two operators for what they are.

Remember that in ECMAScritp numeric literals can only be positive (or
zero) and the unary negation operator in, for example, -10 is applied at
runtime (though 'optimising' such an occurrence in source code into a
negative numeric value prior to execution of any code in which it appears
would be possible, and would have no consequences).

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top