PDA

View Full Version : Auto calculate the subtraction of total earn and total deduction



newphpcoder
10-18-2011, 05:41 AM
Good day!


I am new in javascript function.

I have Javascript code for auto calculate

here is the code:


<script type="text/javascript" language="javascript">

function autocalearn(oText)
{
if (isNaN(oText.value)) //filter input
{
alert('Numbers only!');
oText.value = '';
}
var field, val, oForm = oText.form, TotEarn = a = 0;
for (a; a < arguments.length; ++a) //loop through text elements
{
field = arguments[a];
val = parseFloat(field.value); //get value
if (!isNaN(val)) //number?
{
TotEarn += val; //accumulate
}
}
var tot=Number(TotEarn) + Number(document.getElementById('Amount').value);
oForm.TotEarn.value = tot.toFixed(2);
//oForm.TotEarn.value = TotEarn.toFixed(2); //out

}

</script>
<!--Total Deduction AutoCompute-->
<script type="text/javascript" language="javascript">

function autocalded(oText)
{
if (isNaN(oText.value)) //filter input
{
alert('Numbers only!');
oText.value = '';
}
var field, val, oForm = oText.form, TotalDed = a = 0;
for (a; a < arguments.length; ++a) //loop through text elements
{
field = arguments[a];
val = parseFloat(field.value); //get value
if (!isNaN(val)) //number?
{
TotalDed += val; //accumulate
}
}
//oForm.TotalDed.value = TotalDed.toFixed(2); //out

var totded=Number(TotalDed) + Number(document.getElementById('Deductions').value);
oForm.TotalDed.value = totded.toFixed(2);
}

</script>


and now my problem is...I have a textbox for the overall total, and i want it automatic subtract the total earn and total deduction..

I will attach my codes for further understanding.

Thank you in advance

newphpcoder
10-19-2011, 08:18 AM
Theres a programmer help me to display and subract the total earn and total deduction, the only problem now is when I add data in total earn it did not add in Over all total.. the only good happen is when I add deductions it take it takes effect or subtract in over all total..

Like for example I add total earn from 1000 i add 20 the total earn become 1020 but the overall total still 100 but when i add also deduction from 100 I add 10 the overall total become 910 which is correct. I only want is when the total earn was change also the overall total will also change..

Thank you

here is the revise code:


<script type="text/javascript">
var tot;
function autocalearn(oText)
{
if (isNaN(oText.value))
{
alert('Numbers only!');
oText.value = '';
}
var field, val, oForm = oText.form, TotEarn = a = 0;
for (a; a < arguments.length; ++a) //loop through text elements
{
field = arguments[a];
val = parseFloat(field.value); //get value
if (!isNaN(val))
{
TotEarn += val;
}
}

tot=Number(TotEarn) + Number(document.getElementById('Amount').value);
oForm.TotEarn.value = tot.toFixed(2);
}

function autocalded(oText)
{
if (isNaN(oText.value))
{
alert('Numbers only!');
oText.value = '';
}
var field, val, oForm = oText.form, TotalDed = a = 0;
for (a; a < arguments.length; ++a) //loop through text elements
{
field = arguments[a];
val = parseFloat(field.value);
if (!isNaN(val)) //number?
{
TotalDed += val;
}
}


var totded=Number(TotalDed) + Number(document.getElementById('Deductions').value);
oForm.TotalDed.value = totded.toFixed(2);
var overallTotal = tot - totded;

oForm.TakeHomePay.value = overallTotal.toFixed(2);
}

</script>



Thank you