how to add 2 values: nb.value and n=2

C

Chris

Hi,

I've a problem to add 2 numbers.

function test (nombre){ <<<< nombre came from a form. ( for exemple 3)

var toto=2;
Sum = nombre.value + toto;
alert(Sum); <<< display 23 intead of 5 !!

}

I suppose that it's easy, but I didn't success to display 5.

Thanks for your help.
Chris
 
R

Randy Webb

G said:
essayez :

var sum=0;
sum += nombre;

Since nombre is passed as a parameter to the function, it is passed as a
string. Failing to take that into consideration can lead to fundamental
errors in code.

The FAQ explains it, see it via sig below.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sun, 8 Aug
2004 21:19:13, seen in G Roydor
essayez :

var sum=0;
sum += nombre;

You should yourself try what you intend to recommend.

Not only is your basic idea at fault, but you have omitted the .value .

Code such as yours gives me, in variable sum, 0[object] .



<FAQENTRY>
Section "2.3 What do I have to do before posting to clj?" is aimed
largely at the enquirer.

ISTM that it would be useful to divide it, like Gaul, into three sub-
sections; the first applying to all articles, the second to initial
questions, and the third to responses - splitting should be a mainly
drag'n'drop edit.

Then to the responses section could be added something expressing a
preference for responses to be either tested or marked as untested.

And to the common section a preference for code to be copy'n'pasted,
rather than typed in, whenever possible.

2.3 para 1, of -> with.
 

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,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top