![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
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 |
|
function addRowToTable() .... // price cell .... el_price.setAttribute('name', 'price' + iteration); |
|
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; } |
#2
| |||
| |||
|
#3
| |||
| |||
|
#4
| |||
| |||
|
|
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 |
#5
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |