# Javascript calculation on form

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Quick questions:

I have a script with a page that totals a price assuming quantity(X)=1 .

Customer have told me to go to quantity > 1 .

I did a test and I got a non-numeric answer and am scratching my head.

Page is found at https://secure.nl2k.ab.ca/pdsolutions/indexnongst.test.html .

--
Member - Liberal International
This is doctor@nl2k.ab.ca    Ici doctor@nl2k.ab.ca
God, Queen and country! Beware Anti-Christ rising!
Birthdate: 29 Jan 1969 Redhill Surrey England

## Re: Javascript calculation on form

I can't see where the error is but that code is going to be very hard to
maintain. Why not simplify it by looping through the table rows and adding
each quantity times price if the checkbox is checked?
Then if you add a new row there's no script change. All you need is to give
the table an ID to be able to get a reference to it (or use
getElementsByTagName if you know how many tables you have.

--

Joe

http://joe.fawcett.name

## Re: Javascript calculation on form

On Jan 27, 2:40=A0am, doc...@doctor.nl2k.ab.ca (The Doctor) wrote:

If the quantity can only ever be zero or one (and the page is coded so
that they are the effectively the only two values that can be used)
all you need is a checkbox, the quantity is irrelevant.

I went for a walk and got a cup of tea and am reclining
peacfully.  ;-)

javascript group?

Pointers:

1. Use a loop to collect values from the form, it is much, much
simpler.

2. Don't generate huge numbers of variables, use object properties or
an array.  It makes dealing with them much simpler, e.g. you can loop
over them rather than explicity coding for each one.

3. Do not use expressions like:

var wprice1 = document.calc.price1.value = price1;
var wquantity1 = document.calc.quantity1.value = quantity1;

I don't think you know what that is doing.

5. Only decalre variables once.  It doesn't hurt to declare them
multiple times, but it might get confusing.

4. To debug, create a very simple version of your page (say with just
two line items) and go from there.   Get some debugging tools and ask
questions in an appropriate forum.

Anyhow, your problem is that you are declaring variables like
wquantity1, but only giving them a value if their associated checkbox
is checked (look at the else part of your if statements).  However,
your total calculation uses them regardless of whether they have been
given a value or not.

If their checkbox isn't checked, wquantity1 et al have a value of
undefined, any number * undefined gives NaN (the special not-a-number
value).  If any number in the calculation resolves to NaN, the whole
expression is NaN.

If you fix that (all 67 instances of it) your code will "work".  See,
a loop would be easier.  :-)

I suggest you seek help on how to write the script much more
efficiently.

--
Rob.

## Re: Javascript calculation on form

[...]

Sorry about that, got confused where I was, thought I was replying to
another group...

--
Rob

## Re: Javascript calculation on form

A nice long walk always helps.

You are not going to like me.  I have seen this in many programming
languages but not Java/Javascript.  How do you start a loop?

So can one do that in said form?

This is what you get when reading the web

Once works for me.

How so in Javascript?

Ugh.

Loops and arrays makes sense.

Any web pointers to this?

--
Member - Liberal International
This is doctor@nl2k.ab.ca    Ici doctor@nl2k.ab.ca
God, Queen and country! Beware Anti-Christ rising!
Birthdate: 29 Jan 1969 Redhill Surrey England

## Re: Javascript calculation on form

On 01/30/09 18:51, The Doctor wrote:

Are you trying to write JavaScript without a JavaScript reference (book)?
I have the O'Reilly book: JavaScript: The Definitive Guide, which shows
(among many, *many* other things) several looping constructs, like

for (initialize; test; increment)
statement

or

initialize;
while (test) {
statement;
increment;
}

## Re: Javascript calculation on form

Shotgun / looking forthe best answer.
--
Member - Liberal International
This is doctor@nl2k.ab.ca    Ici doctor@nl2k.ab.ca
God, Queen and country! Beware Anti-Christ rising!
Birthdate: 29 Jan 1969 Redhill Surrey England