Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-24-2002, 03:23 PM   PM User | #1
askomski
New to the CF scene

 
Join Date: Jun 2002
Location: Livonia, MI
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
askomski is an unknown quantity at this point
Addition in a form

I have developed the following script:

function calcBudget(){
a = CapBudget;
b = ExpBudget;
total = a + b;
return total;}

Example:
a = 47 b = 53
Answer should be 100, but I get 4753. How can I add these two together as numbers?

This is for a Intranet site at work. Calculation must be done on the client side.
askomski is offline   Reply With Quote
Old 06-24-2002, 03:43 PM   PM User | #2
QuackHead
Regular Coder

 
Join Date: Jun 2002
Posts: 344
Thanks: 0
Thanked 0 Times in 0 Posts
QuackHead is an unknown quantity at this point
It works fine for me, however I think your problem may be this...

when you're defining CapBudget and ExpBudget, are you putting the values in Quotes?

CapBudget = '47';
ExpBudget = '53';

Just define those to variables without the quotes, and you'll be fine.

CapBudget = 47;
ExpBudget = 53;
...
...
total = 100


~Quack
QuackHead is offline   Reply With Quote
Old 06-24-2002, 03:58 PM   PM User | #3
askomski
New to the CF scene

 
Join Date: Jun 2002
Location: Livonia, MI
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
askomski is an unknown quantity at this point
Quackhead,

Thanks for the reply. I apologize, but I may not have made it clear in my example that the user will be entering the values into the form, so the 47 and 53 would be entered into the form. So I really don't have a place to put the quotes since the numbers are not hard coded in. So maybe my example should have been:

a = document.form.CapBudget.value
b = document.form.ExpBudget.value
total = a + b;

return total;

With the return giving a field called TotBudget the result. As I stated, the strings are being combined, but the addition is not happening. This is being done on an Intranet site and the browsers being used are Netscape 4. I may be unclear how the script determines whether the user entry is text or a number.
askomski is offline   Reply With Quote
Old 06-24-2002, 04:00 PM   PM User | #4
tamienne
Regular Coder

 
Join Date: Jun 2002
Location: Delaware, USA
Posts: 138
Thanks: 0
Thanked 0 Times in 0 Posts
tamienne is an unknown quantity at this point
Use Number to convert it to a number.

a = Number(document.form.CapBudget.value);
b = Number(document.form.ExpBudget.value);
total = a + b;

return total;
tamienne is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:28 AM.


Advertisement
Log in to turn off these ads.