473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Script to Add and Count Values

Hi all,

I'm new to JavaScript, but am pretty sure what I want to accomplish is
not that difficult. I just need an example or suggestion to help
clarify it for me - I haven't had much time to learn JS, so I have
been using some prebuilt scripts where needed.

I have a Web page form where a user can enter data. The first step is
for them to select a dropdown menu to indicate the number of entries
they will be making (the dropdown menu provides options of 5, 10, 25,
50, and 100). They may or may not actually enter that number (in other
words, they may choose 5 and only enter 2, so there would be 3 blank
rows). This then gives them a series of blank text boxes and they
enter the appropriate information (first name, last name, SSN, hours,
wages), such as:

Tim Jones 123-45-6789 40.0 1,275.00
Bob Stark 345-67-8901 32.0 1,002.75
(blank)
(blank)
(blank)
---------- ---- --------
Totals: 2 72.0 2,277.75

At the bottom of the form is a row where I'd like to provide totals
(dynamically) as they enter data. The first total would be the # of
names entered, the second total would be hours, and the third total
would be wages.

I need a script that will count the number of rows in the text column
(with data only) and add the values in the numeric columns, changing
the value in the Totals row as entries are made (or removed).

Any examples or suggestions are greatly appreciated.

TIA,
Yellowbird
Jul 20 '05 #1
5 2937
In article <63**************************@posting.google.com >,
ye*************@hotmail.com enlightened us with...

I need a script that will count the number of rows in the text column
(with data only) and add the values in the numeric columns, changing
the value in the Totals row as entries are made (or removed).

Any examples or suggestions are greatly appreciated.

The best way to do this is to name all the boxes you want in the total
with a particular name, then loop through the form when the element
changes and get the total of the ones that have numbers in them.

For example, you name all the boxes you want in the total with "T_" and
NO other elements have that in the name.
So you have
T_1
T_2
T_3
T_4
T_5
....
and so on.

Each one of these has an onChange that calls a function, call it
getTotals() or something.
<input type="text" name="T_1" onChange="getTotals()">

I'll assume the form is named form1 for convenience.
I'll assume the total element where you want it written is named
"total".
(watch for word-wrap)

function getTotals()
{
var total = 0;
var L = document.forms["form1"].elements.length;
for (var i=0; i<L; i++)
{
if (document.forms["form1"].elements[i].name.indexOf("T_") -1 && !isNaN(document.forms["form1"].elements[i].value))

{
total += parseFloat(document.forms["form1"].elements
[i].value);
}
}
document.forms["form1"].elements["total"].value = total;
return;
}

I didn't test this, so there may be a typo. It's early. :)

--
--
~kaeli~
She was engaged to a boyfriend with a wooden leg but broke
it off.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #2
Thanks, Kaeli. I think I get the gist of it, although I still have
some work to do. This sort of works on my non-numeric first column
(where I am simply counting the number of rows with text entered), but
the total field of the non-numeric column displays a 0 (zero), so it
doesn't really sum things up. When I tried this on my numeric column,
the numeric column total fields (hours total and wages total) display
"NaN", which doesn't make sense since the value in this field is a
number. I think I need a way to total the non-numeric column and the
numeric columns using the same script. However, if I go with the "T_"
naming convention, I get erratic results. Do I need individual scripts
for each column I want to total? I guess eventually I will also need
to add some validation to check for non-numeric values in my numeric
column (and vice versa).

Again, thanks for any suggestions or guidance,
Yellowbird
Jul 20 '05 #3
In article <63**************************@posting.google.com >,
ye*************@hotmail.com enlightened us with...
Thanks, Kaeli. I think I get the gist of it, although I still have
some work to do. This sort of works on my non-numeric first column
A total will not work if the value is not a number. This script assumes
all values are numeric in the T_ inputs. It will not sum non-numerics.
(where I am simply counting the number of rows with text entered), but
the total field of the non-numeric column displays a 0 (zero), so it
doesn't really sum things up. When I tried this on my numeric column,
the numeric column total fields (hours total and wages total) display
"NaN", which doesn't make sense since the value in this field is a
number.
How are they formatted?
Is there a dollar sign or a period?
I think I need a way to total the non-numeric column and the
numeric columns using the same script.


You can't sum a non-numeric. Letters and symbols aren't numbers.

If you have a URL, that would be helpful...

--
--
~kaeli~
Dijon vu - the same mustard as before.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #4
kaeli <ti******@NOSPAM.comcast.net> wrote in message news:<MP************************@nntp.lucent.com>. ..
A total will not work if the value is not a number. This script assumes
all values are numeric in the T_ inputs. It will not sum non-numerics.


I realized that after I started working with it. I've actually come up
with a new concept for this part of the form. Originally I had the
user select a radio button to indicate the number of rows to complete
and then I displayed that number for them to complete. Now I'm
thinking that it might be better if I can have one part of the form
with a single row where they enter data, then click a button to add it
to the "master list" at the bottom of the form. They'd have to do that
every time they wanted to add something to the list, and would receive
a confirmation before they submit - at this point they can edit or
delete the entries in the list. I can then capture the total number of
rows in a different manner.
When I tried this on my numeric column,
the numeric column total fields (hours total and wages total) display
"NaN", which doesn't make sense since the value in this field is a
number.


How are they formatted?
Is there a dollar sign or a period?


I have them formatted as decimals with the explicit period,
right-justified (so, for example, if they enter 40 hours, the value is
formatted as 40.0 and if they enter 1250 for wages, it is formatted
1250.00).

Once I've gotten a little further with this, I'll post an example and
probably ask the group for some additional feedback/suggestions.

Thanks again,
Yellowbird
Jul 20 '05 #5
In article <63**************************@posting.google.com >,
ye*************@hotmail.com enlightened us with...
I can then capture the total number of
rows in a different manner.

Why do you need the total rows? I can't see a reason for it. You don't
need it for a total if they have the right naming convention.
I have them formatted as decimals with the explicit period,
right-justified (so, for example, if they enter 40 hours, the value is
formatted as 40.0 and if they enter 1250 for wages, it is formatted
1250.00).

ParseFloat should work fine then...
Once I've gotten a little further with this, I'll post an example and
probably ask the group for some additional feedback/suggestions.


That would help us know why you're getting NaN.
The only thing I can think of is that you're grabbing a non-numeric
field and trying to total it. Periods are allowed in float, so there is
something else going on here.

--
--
~kaeli~
A backward poet writes... inverse.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Brian Murphy | last post by:
<?php /* I need your help. I'd be very thankfull if write me this script.I need a script that displays a list of categories and subcategories like this: <select name="category"> <option...
0
by: infinull | last post by:
I am making a script that is a very complex Content Management system. it makes a UDM navigation bar and writes content and a title. i want it to write content other that just basic text, like a...
7
by: Adam | last post by:
Hi, I am working on a project that requires me to parse 52,005,167 rows in a MySQL table. I am using a series of PHP scripts for each test I would like to run on the data. For example I have...
9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
1
by: Chris Todhunter | last post by:
Hi, I am responsible for a growing website. It is password protected and we collect data on everyone who logs in. Every month we produce a report on the trends of users which can result in some...
4
by: none | last post by:
Below is an old count-up script that displays ok in MSIE with a bit of experimenting, but NS shows negative values that make no sense. Anyone know how to make it work ok in both? === Cut ===...
20
by: none | last post by:
I have managed to get the below script *almost* working. However, it still has a problem calculating the number of months. The date I am trying to calculate from is Oct 15, 1994. With the correct...
15
by: Papajo | last post by:
Hi, This script will write a random number into a document write tag, I've been trying to get it to write into a input form box outside the javascript, any help is appreciated. Thanks Joe ...
9
by: perls | last post by:
"Newbie needs help" Hi all, I had a programmer do a site scraping script for me.. the aim was to scrape data from 5 different sites and upload directly into my website databse. I started to...
15
by: Lawrence Krubner | last post by:
Does anything about this script look expensive, in terms of resources or execution time? This script dies after processing about 20 or 25 numbers, yet it leaves no errors in the error logs. This is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.