Table Calculations

Modify the function tallyRow() to update the total column value at the bottom of table.


Originals and Lithographs
Item # Description Price $ Quantity Row Total $
100001 The Dragon
100002 Hush Little Ones
100003 A Monster, A Mouse, The Princess and Me
100004 Laura's Song
100005 The Christmas Bear of Baffin Bay
100006 Boxes and Bows
        Total    

The Code

function tallyRow() { z=document.orderform.countTtl.value; // hidden value of db recordset count //alert(z) // use for testing x=0;y=0;Cost=0;Total=0;ttlQuant=0; // Globals for (i=1;i<=z;i++) // loop through rows { // Use Eval function to dynamically build the column name x=eval("document.orderform.quantity_"+ i +".value"); // x is quantity y=eval("document.orderform.price_"+ i +".value"); // y is price Cost = (x*y); // simple calcualtion //Pass value of calculation back as 2 decimal value - use __.toFixed(2) function //nX = nX.toFixed(2) document.getElementById("row_ttl_" + i ).value = Cost.toFixed(2); Total=Total+Cost; // Add new col total to old col total document.orderform.Total.value = Total.toFixed(2); // Pass value to table } } . . . H T M L . . . <form name="orderform" id="orderform" method="get"> <input type="hidden" id="countTtl" name="countTtl" value="6"> <table class="tbl_1" width=95%> <tr> <th colspan=5 > Originals and Lithographs </th> </tr> <tr> <td style="width:50px;"> Item # </td> <td style="width:250px;"> Description </td> <td style="width:50px;"> Price $ </td> <td style="width:50px;"> Quantity </td> <td style="width:100px;"> Row Total $ </td> </tr> <tr> <td > 100001 </td> <td > The Dragon </td> <td > <input type="text" class="price_ttl_1" name="price_1" id="price_1" value="500.00" readonly="true" /> </td> <td > <input type="text" class="" name="quantity_1" id="quantity_1" value="" size="3" onblur="tallyRow();" /> </td> <td > <input type="text" class="price_ttl_1" name="row_ttl_1" id="row_ttl_1" value="0.00" readonly="true" /> </td> </tr> <tr> <td > 100002 </td> <td > Hush Little Ones </td> <td > <input type="text" class="price_ttl_2" name="price_2" id="price_2" value="600.00" readonly="true" /> </td> <td > <input type="text" class="" name="quantity_2" id="quantity_2" value="" size="3" onblur="tallyRow();" /> </td> <td > <input type="text" class="price_ttl_2" name="row_ttl_2" id="row_ttl_2" value="0.00" readonly="true" /> </td> </tr> <tr> <td > 100003 </td> <td > A Monster, A Mouse, The Princess and Me </td> <td > <input type="text" class="price_ttl_1" name="price_3" id="price_3" value="450.00" readonly="true" /> </td> <td > <input type="text" class="" name="quantity_3" id="quantity_3" value="" size="3" onblur="tallyRow();" /> </td> <td > <input type="text" class="price_ttl_1" name="row_ttl_3" id="row_ttl_3" value="0.00" readonly="true" /> </td> </tr> <tr> <td > 100004 </td> <td > Laura's Song </td> <td > <input type="text" class="price_ttl_2" name="price_4" id="price_4" value="300.00" readonly="true" /> </td> <td > <input type="text" class="" name="quantity_4" id="quantity_4" value="" size="3" onblur="tallyRow();" /> </td> <td > <input type="text" class="price_ttl_2" name="row_ttl_4" id="row_ttl_4" value="0.00" readonly="true" /> </td> </tr> <tr> <td > 100005 </td> <td > The Christmas Bear of Baffin Bay </td> <td > <input type="text" class="price_ttl_1" name="price_5" id="price_5" value="650.00" readonly="true" /> </td> <td > <input type="text" class="" name="quantity_5" id="quantity_5" value="" size="3" onblur="tallyRow();" /> </td> <td > <input type="text" class="price_ttl_1" name="row_ttl_5" id="row_ttl_5" value="0.00" readonly="true" /> </td> </tr> <tr> <td > 100006 </td> <td > Boxes and Bows </td> <td > <input type="text" class="price_ttl_2" name="price_6" id="price_6" value="700.00" readonly="true" /> </td> <td > <input type="text" class="" name="quantity_6" id="quantity_6" value="" size="3" onblur="tallyRow();" /> </td> <td > <input type="text" class="price_ttl_2" name="row_ttl_6" id="row_ttl_6" value="0.00" readonly="true" /> </td> </tr> <tr> <td colspan="2" align="center"> <input type="button" value="Recalculate" onclick="tallyRow();"> &nbsp;&nbsp;&nbsp; <input type="button" value="Submit" onclick="popup1();"> &nbsp;&nbsp;&nbsp; <input type="reset" > </td> <td colspan=2 align=right> Total &nbsp;&nbsp;&nbsp; </td> <td class="td_0"> <input type="text" class="ttl" name="Total" id="Total" value="0.00" readonly="true" /> </td> </tr> </table> </form>
Back     Lectures     Next