Page 1 of 1

Javascript Sum numbers

Posted: Tue Sep 30, 2008 11:36 am
by arunkar
Hi,

Im trying to calulate the sum of the checked boxes with simple javascript.

I have a 3 check boxes. On selecting two (ambassador_pack, business_ambassador_licence) of them the sum is auto calculated and stored in TotalAmount.

One check box has a text box where the user enters the quantity for the prodects... quantity * cost(95) is calculated and added to the TotalAmount.

I get Error, when I keyin the quantity: "Error: document.getElementById("kids_ambassador_pack_qty") is null"

Code: Select all

 
<input type="checkbox" id="ambassador_pack" name="ambassador_pack" value="195"  onclick="readAmPak('ambassador_pack');" />  
Ambassador Pack $195</label>
    <br/>
    
    <label><input type="checkbox" id="kids_ambassador_pack" name="kids_ambassador_pack" value="95" > Kids Ambassador Pack</label>
    ($95 per pack)
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$95 x
     <input type="text" name="kids_ambassador_pack_qty" value="" size="5" onchange="readQty('kids_ambassador_pack_qty');" //>     enter  qunatity<br/> 
    
    <label><input type="checkbox" id="business_ambassador_licence" name="business_ambassador_licence" value="1000" onchange="readAmlic('business_ambassador_licence');" > 
    Business Ambassador Licence $4850</label>
    [<label>Initial 'Hold-a-slot-for-Me' $1000]</label>
 
<br><br>
Total Amount
              <input name="total_amount" type="text" id="total_amount"  style="background: #FF9900; text-align:right; font-weight:bold; color:black;" size='12' maxlength="50" disabled/>
 
javascript:

Code: Select all

 
function readAmPak(qtyfield){
    
    if(document.frmSS17.ambassador_pack.checked == true){
     
        if (document.getElementById("total_amount").value ==""){
            subTotal = parseInt(document.getElementById("ambassador_pack").value);
        }else{
            subTotal = parseInt(document.getElementById("total_amount").value) + parseInt(document.getElementById("ambassador_pack").value);
        }
        document.getElementById("total_amount").value = Math.round(subTotal*100)/100 ;
    }
//--------
    if(document.frmSS17.ambassador_pack.checked == false){
     
        if (document.getElementById("total_amount").value ==""){
            subTotal = parseInt(document.getElementById("ambassador_pack").value);
        }else{
            subTotal = parseInt(document.getElementById("total_amount").value) - parseInt(document.getElementById("ambassador_pack").value);
        }
        document.getElementById("total_amount").value = Math.round(subTotal*100)/100 ;
    }
}
    
function readQty(qtyfield){
        
    document.frmSS17.kids_ambassador_pack.checked = true;
    
    if (!(document.getElementById("kids_ambassador_pack_qty").value =="")){
        var qtykidam_pk = document.getElementById("kids_ambassador_pack_qty").value; 
        qtykidam_pk = qtykidam_pk * 1;
            alert("kids_ambassador_pack_qty "+document.getElementById("kids_ambassador_pack_qty").value);
 
            if (isNaN(qtykidam_pk)) {
              qtykidam_pk = 0;
              alert("Months have to be numerical");
              document.getElementById("kids_ambassador_pack_qty").focus();
            }
            
        subTotal = parseInt(document.getElementById("total_amount").value) + (95 * parseInt(document.getElementById("kids_ambassador_pack_qty").value));
        
        document.getElementById("total_amount").value = Math.round(subTotal*100)/100;
    }
    alert("kids_ambassador_pack_qty "+document.getElementById("kids_ambassador_pack_qty").value);
}
 
function readAmlic(qtyfield){
    
    if(document.frmSS17.business_ambassador_licence.checked == true){
     
        subTotal = parseInt(document.getElementById("total_amount").value) + parseInt(document.getElementById("business_ambassador_licence").value);
        
        document.getElementById("total_amount").value = Math.round(subTotal*100)/100;
    }
//--------
    if(document.frmSS17.business_ambassador_licence.checked == false){
     
        if (document.getElementById("total_amount").value ==""){
            subTotal = parseInt(document.getElementById("business_ambassador_licence").value);
        }else{
            subTotal = parseInt(document.getElementById("total_amount").value) - parseInt(document.getElementById("business_ambassador_licence").value);
        }
        document.getElementById("total_amount").value = Math.round(subTotal*100)/100;
    }
}
 
 

Any suggestion/ ideas how to fix this error guys?


Thanks, much appreciated

Re: Javascript Sum numbers

Posted: Tue Sep 30, 2008 1:04 pm
by andyhoneycutt
arunkar wrote:

Code: Select all

 
<input type="checkbox" id="ambassador_pack" name="ambassador_pack" value="195"  onclick="readAmPak('ambassador_pack');" />  
Ambassador Pack $195</label>
    <br/>
    
    <label><input type="checkbox" id="kids_ambassador_pack" name="kids_ambassador_pack" value="95" > Kids Ambassador Pack</label>
    ($95 per pack)
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$95 x
     <input type="text" name="kids_ambassador_pack_qty" [color=#FF0000]id="kids_ambassador_pack_qty" [/color]value="" size="5" onchange="readQty('kids_ambassador_pack_qty');" [color=#FF0000]/ (extra slash)[/color]/>     enter  qunatity<br/> 
...

Re: Javascript Sum numbers

Posted: Wed Oct 01, 2008 12:30 am
by arunkar
Thanks andyhoneycutt :)

It worked. but why did we add the / (extra slash) in line 10 ?

I have a logical error in the quantity field. When I enter the below values in the quantity field the I get strange output:

qty timesvalue --> totalAmount
entered
1 * 95 --> 95 (good works)
2 * 95 --> 285 (strange increases by 190)
3 * 95 --> 475(wrong)
4 * 95 --> 855 (wrong)
2 * 95 --> 1045(wrong)

Even when I decrease the quantity the result keeps totalAmount

This is my code:

Code: Select all

 
 
 
function readKdAmPk(qtyfield){
    
    if(document.frmSS17.kids_ambassador_pack.checked == true){
     
        if (document.getElementById("kids_ambassador_pack_qty").value ==""){
            document.getElementById("kids_ambassador_pack_qty").value =1;
        }
        
        if (document.getElementById("total_amount").value ==""){
            subTotal = 95*(document.getElementById("kids_ambassador_pack_qty").value);
        }else {
            subTotal = parseInt(document.getElementById("total_amount").value) + (95 * parseInt(document.getElementById("kids_ambassador_pack_qty").value));
        }
        
        document.getElementById("total_amount").value = Math.round(subTotal*100)/100 ;
    }
//--------
    if(document.frmSS17.kids_ambassador_pack.checked == false){
     
        if (document.getElementById("total_amount").value ==""){
            subTotal = 0;//95*(document.getElementById("kids_ambassador_pack_qty").value);
        }else {
            subTotal = parseInt(document.getElementById("total_amount").value) - (95 * parseInt(document.getElementById("kids_ambassador_pack_qty").value));
        }
    
    //if (document.getElementById("kids_ambassador_pack_qty").value ==""){
            document.getElementById("kids_ambassador_pack_qty").value =0;
    //}
        
        document.getElementById("total_amount").value = Math.round(subTotal*100)/100 ;
    }
}
 
 
Any suggestions and Ideas greatly appericated.

Thanks

Re: Javascript Sum numbers

Posted: Thu Oct 02, 2008 12:25 pm
by andyhoneycutt
I was noting that the second slash should be removed. It existed in the code you posted in the OP but shouldn't be there. I'll take a look at the new problem you are seeing.

-Andy