JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 3.50 average. Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 5th, 2001, 11:58 AM
Zak Hanano Zak Hanano is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 2 Zak Hanano User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up Adding up text fields in Javascript

Hello all - I am new to this forum as I am new to the world of JS. I wanted to know if anyone can point me in the right direction on how to add JS to add up multiple fields in a HTML form I have created.

I use the eval() function to change entered text (in the fields in question) to numbers and the initial values of these fields is set to "" (value=""). My problem is how do I incorporate the empty fields (value="", i.e. the fields that have been left blank) to default to zero so that the addition function I created works?

Right now, after entering values in all the fields results in the right answer, but leaving one field empty results in NaN. I also would like not to have to use a default (value="0") of zero in those fields as it doesnt look as nice. Any help is greatly appreciated.

small Sample Code:
I have a series of Input statements 1-7 that look like this:
<input name="Amount1" size="10" value="" maxlength="10" onblur="doTotal(this.form)">

<input name="Amount2" size="10" value="" maxlength="10" onblur="doTotal(this.form)">

<input name="Amount3" size="10" value="" maxlength="10" onblur="doTotal(this.form)">

<input name="Amount4" size="10" value="" maxlength="10" onblur="doTotal(this.form)">

<input name="Amount5" size="10" value="" maxlength="10" onblur="doTotal(this.form)">

etc. etc. etc.

I would like to add these fields up into a "Total" field
<input readonly name="Total" size="10" value="" maxlength="10" >

My JS code looks like:
function doTotal(form) {
a=eval(form.Amount1.value);
b=eval(form.Amount2.value);
c=eval(form.Amount3.value);
d=eval(form.Amount4.value);
e=eval(form.Amount5.value);
f=eval(form.Amount6.value);
g=eval(form.Amount7.value);
form.Total.value = a + b + c + d + e + f + g;
}

Last edited by Zak Hanano : June 5th, 2001 at 12:15 PM.

Reply With Quote
  #2  
Old June 5th, 2001, 12:04 PM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 13
best thing to do is to post a code sample - makes it easier to put a reply together...

Reply With Quote
  #3  
Old June 5th, 2001, 09:46 PM
adios adios is offline
Senior Citizen
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2001
Location: leftcoast
Posts: 2,019 adios User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 15
function doTotal(form) {
var a = (form.Amount1.value != '') ? eval(form.Amount1.value) : 0;
var b = (form.Amount2.value != '') ? eval(form.Amount2.value) : 0;
var c = (form.Amount3.value != '') ? eval(form.Amount3.value) : 0;
var d = (form.Amount4.value != '') ? eval(form.Amount4.value) : 0;
var e = (form.Amount5.value != '') ? eval(form.Amount5.value) : 0;

form.Total.value = a + b + c + d + e ;
}

Replace the onblur handlers with onchange as well.

http://developer.netscape.com/docs/...pr.html#1008690

Last edited by adios : June 6th, 2001 at 08:24 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Adding up text fields in Javascript

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap