Hi all,
I have a javascript that takes values of textboxes and calculates them out. What I need to do is tweak my script so I can use multiple "products". I was thinking of appending a unique string in the beginning of the name/id of each textbox then using match expression or preg (??? not sure if they are correct terms) to have the script find the unique sets and calculate them accordingly.
ie.
set1= QTY11_somename * PRICE11_somename
set2= QTY5999_somename * PRICE5999_somename
set3=QTYd_somename * PRICEd_somename
then have the script take the sum of all of the sets to calculate the totals...
In my script so far, the names are hard coded (only 1 set). I need multiple sets with 1 set of totals... see my example:
Thank you for your reply. That is really close. I can't have a total and tax for each set. I need to have a tax_total, a sub_total (total without tax) then a grand_total (with tax) once at the end. If you have any more suggestions, I would be happy to see them. I am going to play with your code and see what I can come up with myself also... Thank you!
something more like this (even though "gtotal" is not working for some reason and I cannot have the <div> set forced to be sequential):
<script type="text/javascript">
function setEvent(){
var but, i=1;
while(but=document.getElementById('calc_'+(i++))){
but.onclick=calculate
}
}
function calculate(){
var f=this.form, grandtotV=0, j=1, i=1, but;
while(but=document.getElementById('calc_'+(j++))){
var oQty=f['qty_'+i];
var oPrice=f['price_'+i];
// var oTaxrate=f['taxrate_'+i];
var oSubtotal=f['subtotal_'+i];
// var oTax=f['tax_'+i];
var oTotal=f['total_'+i];
i++;
var subV=Number(oQty.value)*Number(oPrice.value);
// var taxV=subV*Number(oTaxrate.value);
// var totV=subV+subV*Number(oTaxrate.value);
var totV=subV;
grandtotV+=totV;
Kor, thank you for your help!!! I am getting there with your code. THis is more of what I wanted (see code). Is there any way to remove the "calculate" button for each div and just use onblur (not sure if using correct termonology) instead...:
<script type="text/javascript">
function setEvent(){
var but, i=1;
while(but=document.getElementById('calc_'+(i++))){
but.onclick=calculate
}
}
function calculate(){
var f=this.form, grandtotV=0, j=1, i=1, but;
while(but=document.getElementById('calc_'+(j++))){
var oQty=f['qty_'+i];
var oPrice=f['price_'+i];
// var oTaxrate=f['taxrate_'+i];
// var oSubtotal=f['subtotal_'+i];
// var oTax=f['tax_'+i];
// var oTotal=f['total_'+i];
i++;
var subV=Number(oQty.value)*Number(oPrice.value);
// var taxV=subV*Number(oTaxrate.value);
// var totV=subV+subV*Number(oTaxrate.value);
var totV=subV;
grandtotV+=totV;
Bookmarks