File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes Java Script Addition in for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Java Script Addition in for loop" Watch "Java Script Addition in for loop" New topic

Java Script Addition in for loop

Rashmi Trivedi
Ranch Hand

Joined: Aug 13, 2001
Posts: 60
Hello frineds
I have one strage issue while i am doig the addition in java script inside foor loop.

Let me explain:
I have fetching records from database depending on the requst(query) submitted by users. Thy dynamic JSP page with the query results is display to the users.

i do have the row count so that i know how many rows have been retrieved from database.

I user this variable in javascript inside for loop to get the value of particular field in speific row.

var temp = document.warehouse.icount.value;
for (var i=1; i <= temp; i++) {
var amount = document.warehouse.elements["amount"+i].value;

} // end for
What i would like to do is add all the amounts and display it on the TotalAmount text box. I tried to do this but i am getting very funny results. Amout do not seem to be adding correctly and i cannot assign to Total Amount Coloumn.

Please help me out if you can guys...much appreciated..

let me know if you have any questions or need more clarification.

thank you in advance.

Rashmi Trivedi
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
How about you have a variable and you add each alue to it? In your code you are just storing the values into a variable and you kep writing over it.

I agree. Here's the link:
subject: Java Script Addition in for loop
jQuery in Action, 3rd edition