this is my function which is calculating values. But if any of the values is empty it does not calculate the total payment value. if there is any simple way to calculate pls let me know

function startCalc(){
interval = setInterval("calc()",1);}

function calc(){
				
 room_charges = document.ipd_bill.room_charges.value ;
  lab_charges = document.ipd_bill.lab_charges.value;
  consultancy_fees = document.ipd_bill.consultancy_fees.value;
  blood_charges = document.ipd_bill.blood_charges.value; 
organ_charges = document.ipd_bill.organ_charges.value; 
discount = document.ipd_bill.discount.value;
document.ipd_bill.total_payment.value = eval(room_charges) + eval(lab_charges) + eval(consultancy_fees) + eval(blood_charges) + eval(organ_charges) - discount;
}

function stopCalc(){
	clearInterval(interval);
		}

Recommended Answers

All 3 Replies

try:

function calc(){
				
 room_charges = isNaN(parseInt(document.ipd_bill.room_charges.value)) ? 0 :  parseInt(document.ipd_bill.room_charges.value);
  lab_charges = isNaN(parseInt(document.ipd_bill.lab_charges.value)) ? 0 :  parseInt(document.ipd_bill.lab_charges.value);
  consultancy_fees = isNaN(parseInt(document.ipd_bill.consultancy_fees.value)) ? 0 :  parseInt(document.ipd_bill.consultancy_fees.value);
  blood_charges = isNaN(parseInt(document.ipd_bill.blood_charges.value)) ? 0 :  parseInt(document.ipd_bill.blood_charges.value); 
organ_charges = isNaN(parseInt(document.ipd_bill.organ_charges.value)) ? 0 :  parseInt(document.ipd_bill.organ_charges.value); 
discount = isNaN(parseInt(document.ipd_bill.discount.value)) ? 0 :  parseInt(document.ipd_bill.discount.value);
document.ipd_bill.total_payment.value = eval(room_charges) + eval(lab_charges) + eval(consultancy_fees) + eval(blood_charges) + eval(organ_charges) - discount;
}

hielo

It is giving the following error. Actually i am get room charges, lab charges values from database using ajax. Then i am doing the total and subracting discount. if any of the charges is not available it gives the error.

Message: 'document.ipd_bill.room_charges.value' is null or not an object

you are probably "erasing" your original field OR the field is not in the DOM when you call the function. Can't tell you for sure without looking at your actual page online!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.