HighDots Forums  

Re: How to calculate a total from a list of cells

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: How to calculate a total from a list of cells in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mscir
 
Posts: n/a

Default Re: How to calculate a total from a list of cells - 04-19-2004 , 09:49 PM






Dennis M. Marks wrote:
Quote:
rick <rmsingh (AT) no_spam_rogers (DOT) com> wrote:
I am try to create a simple form using a table, where a user can fill
outquanty and price and have a total automatically calculated and inserted in
another field. I stuck trying to figure out how expand this script to
recalculate when rows are added or removed.

script language="JavaScript" src="/javascript/testtables.js"></script
As Dennis said, use <script type="text/javascript">

Quote:
function addRowToTable()
....
// price cell
....
el_price.setAttribute('name', 'price' + iteration);
To be consistent with the current table, and allow looping in the
total_cost function, use 'cost' instead of 'price':

el_price.setAttribute('name', 'cost' + iteration);

Quote:
function calc_total() {
var num_of_units = document.myform.qty1.value;
var price_of_units = document.myform.cost1.value;
var total_cost = eval(num_of_units * price_of_units)
document.myform.total.value = total_cost;
}
Now you can loop through all but the last table rows to get the total:

function calc_total() {
var rowcount = document.getElementById('itemlist').rows.length;
var subtotal=0;
for (i=0; i<rowcount-1; i++)
subtotal += (document.myform.elements('qty'+i).value *
document.myform.elements('cost'+i).value);
document.myform.total.value = subtotal;
}

Mike



Reply With Quote
  #2  
Old   
mscir
 
Posts: n/a

Default Re: How to calculate a total from a list of cells - 04-19-2004 , 10:56 PM






Revision:

- I had trouble getting setAttribute to work - 'name=qtyN' 'name=costN'
weren't showing up in the table html, so I used the cell innerHTML to
create the input textboxes instead.

- I had to change the value of iteration to get the looping in
calc_total to work, same with deleting row 1 instead of the lastrow-1.

- re-calc whenever there's a number of rows change

....here's what I have so far, maybe give this a try.

function addRowToTable() {
var tbl = document.getElementById('itemlist');
var iteration = tbl.rows.length-1;
var row = tbl.insertRow(1);
var qty = row.insertCell(0); // qty cell
qty.innerHTML='<input type="text" name=qty'+iteration+' size=7
maxlength=7 onChange="calc_total()">';
var price = row.insertCell(1); // price cell
price.innerHTML='<input type="text" name=cost'+iteration+' size=7
maxlength=4 onChange="calc_total()">';
calc_total();
}

function removeRowFromTable() {
var tbl = document.getElementById('itemlist');
var last_Row = tbl.rows.length;
if (last_Row > 2)
tbl.deleteRow(1);
calc_total();
}

function calc_total() {
var rowcount = document.getElementById('itemlist').rows.length-1;
var subtotal=0;
var currcost=0;
var currqty=0;
for (var i=0; i<rowcount; i++) {
currqty = document.myform.elements('qty'+i).value;
currcost = document.myform.elements('cost'+i).value;
if ((currqty == "") || (currcost == "")) {
//ignore this row, it includes blanks
} else {
subtotal += (currqty * currcost);
}
}
document.myform.total.value = subtotal;
}

function showtableinnerhtml() {
alert(document.getElementById('itemlist').innerHTM L);
}


Reply With Quote
  #3  
Old   
rick
 
Posts: n/a

Default Re: How to calculate a total from a list of cells - 04-20-2004 , 04:18 PM




Thanks for all the help, I will try it tonight.



Reply With Quote
  #4  
Old   
rick
 
Posts: n/a

Default Re: How to calculate a total from a list of cells - 04-20-2004 , 04:48 PM




Quote:
Now you can loop through all but the last table rows to get the total:

function calc_total() {
var rowcount = document.getElementById('itemlist').rows.length;
var subtotal=0;
for (i=0; i<rowcount-1; i++)
subtotal += (document.myform.elements('qty'+i).value *
This line is causing an error in Internet Explorer and Firefox, here it is
Error: document.myform.elements is not a function

Quote:
document.myform.elements('cost'+i).value);
document.myform.total.value = subtotal;
}

Mike




Reply With Quote
  #5  
Old   
rick
 
Posts: n/a

Default Re: How to calculate a total from a list of cells - 04-20-2004 , 07:01 PM



Thanks mscir I've the idea now, I appreciate your help

Rick



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.