Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird...

Status
Not open for further replies.

buzzt

Programmer
Oct 17, 2002
171
CA
For some reason every time I do this:

b=document.the_form.days.value;
x=(6 + b);
document.the_form.result.value=x;

Instead of getting a sum, it places the numbers beside each other(i.e. if b=1, I get 61).


If I do this:

b=document.the_form.days.value;
x=(6 * b);
document.the_form.result.value=x;

It works the way it should and multuplies the values. What's wrong here?
 
value is string, + is also string operator, javascript performs some implicit casting and result is a string. Use parseInt(b) or parseFloat(b).
 
Because JS is an untyped language, these things can happen. Use parseInt (or parseFloat) to "force" JS to think that "b" is a number:

Code:
x = 6 + parseInt(b, 10);

or the "cheats" version:

Code:
x = 6 + (b - 0);

Hope this helps,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top