Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript form calculation

Status
Not open for further replies.

ukigeezer

Programmer
Jan 17, 2007
2
GB
I am trying to use javascript to calculate a total in a form of 45 items. The page may be viewed at I have managed to get the calculation to work on a seperate page where there are only four items listed. This may be viewed at I have spent days trying to find a solution but would welcome input from the experts.
Thanks in advance
 
You should try putting alerts throughout your code to try and see at what point it's breaking. That code is a pretty big mess, and using alerts to see what's being stored in the variables is where I'd start. There's a lot of things you could do different (getElementsByTagName("input") to pull the values using a loop comes immediately to mind).

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
A quick scan reveals these.

[1] You have two inputs named q9771a (and also consequently the variable of the same name is assigned twice, but that by itself is no problem.) Main problem is that two input of the same name must be corrected, else the reference to it will not be cast to number by multiplying with 1.

[2] These are other problems, on the face of them unrelated to the NaN return problem in [1], which must be corrected.

[2.1] You have an input named "name", this _must_ be avoided. Name is a reserved word. Apart from that being reserved, if you use it, the property reference obj.name will return unexpected result.

[2.2] You have the form name "form". Again no. You pass the object this.form to the function. You see the risk you are taking.

[2.3] You rely on automatic type casting by multiplying 1 to the value of the input control. You must be aware it is not equivalent to validating them. If it is not a "numeric" string, it still gives you NaN. Furthermore, at the grand total, you use parseInt() again. This is useless. It NaN is already return in the variable, parseInt() will not help. Hence it is there without purpose and too late. It is pure inflation. You should tidy up that part of the logic.

Many other stuff sometimes related to style I would not bother to advance opinions on.
 
Thank you both for your responses. I will take your comments on board.
I selected programmer as my description. You probably both noticed that I should have selected "programmer with limited talent". ;-)
Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top