javascript_howto thread: Fw: Re: simple calulation of form total (Error Corrected)
Sorry ,The code has an error
here it was fixed
Becky :
you must use the same name to all inputs, on this way you 'll
have a collections of inputs. The identity of inputs is by id .
Do this with the HTML
<input type="text" id="numberheard1" name=heards>
<input type="text" id="numberheard2" name=heards>
<input type="text" id="numberheard3" name=heards>
In then script
function totalheards() {
var total=0;
oheards= document.getElementsByName("heards");
for(i=0;i < oheards.length;i++)
total=total+parseInt(oheards[i].value);
return total;
}
and use this function on a button or in code to make the calculation when
you need
----- Original Message -----
From: "Becky Holmes" <rebeholm@h...>
To: "JavaScript HowTo" <javascript_howto@p...>
Sent: Wednesday, April 03, 2002 7:51 PM
Subject: [javascript_howto] Re: simple calulation of form total
> My html looks like this....etc.
> <input type="text" name="numberheard1">
> <input type="text" name="numberheard2">
> <input type="text" name="numberheard3">
>
> I don't have the same name for all records. I can't because each textbox
> is independent of one another. The user is entering the number of birds
> they hear at each stop along a survey route. Last year, I had them enter
> the total birds heard along the whole survey route themselves. However,
> their addition skills were pretty bismal and I ended up double checking
> that data after it was already submitted. I would rather the computer
> enter the "total heard" automatically. If javascript can't complete this
> task is there some other way I can do this? Maybe hit a command button
> and it will add up the values
> for "numberheard1","numberheard1","numberheard3", etc? Thanks.
>
> > Hi Becky: I hope this can help you
> Is your HTML like this:
> <INPUT id=id1 name=price>
> <INPUT id=id2 name=price>
> Attention: Input's names must be the same
> to sum a price field
> ----------------
> var acum;
> oPrice=document.getElementsByName("price");
> for(i=0;i < oPrice.length;i++)
> acum=acum+ Price[i].value;
> P.S : Sorry my English.
>
> ----- Original Message -----
> From: "Becky Holmes" <rebeholm@h...>
> To: "JavaScript HowTo" <javascript_howto@p...>
> Sent: Wednesday, April 03, 2002 5:42 PM
> Subject: [javascript_howto] simple calulation of form total
>
>
> > I have a form with 20 rows of text boxes each with a different field
> > name. The user can enter any number value into the text boxes. I want
a
> > text box at the bottom which automatically totals up all the values as
> > they are typed in within the 20 textboxes. Is there simple javascript
> > that can do this? Thanks.
> >
> > ---
> >
> > Improve your web design skills with these new books from Glasshaus.
> >
> > Usable Web Menus
> > http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
> > r-20
> > Constructing Accessible Web Sites
> > http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
> > r-20
> > Practical JavaScript for the Usable Web
> > http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
> > r-20
> >
>
>
> ---
>
> Improve your web design skills with these new books from Glasshaus.
>
> Usable Web Menus
> http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
> r-20
> Constructing Accessible Web Sites
> http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
> r-20
> Practical JavaScript for the Usable Web
> http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
> r-20
>