PDA

View Full Version : calculate sum of all text boxes

ishakya
22 May 2011, 9:06 PM
Hi,
I need to calculate the sum of all text boxes(which user is entered) to a single text box to display.
I tried many ways but couldn't fulfill it.

I'm in a big trouble.So can anyone please help me to solve this...Any code will be great..

danguba
22 May 2011, 11:53 PM
2. just loop them and call method getValue()

ishakya
23 May 2011, 1:49 AM
can u explain it more please??Because I have a less knowledge in javascript...

danguba
23 May 2011, 1:57 AM
It's quite basic, but if you paste sample of your code I'll try to help you. And don't forget to put it in CODE tags

ishakya
26 May 2011, 11:18 PM
Hi danguba,
I have done a simple function to do that.I think its not good, but working fine.
Calculated total is displayed in a different text box.
This is the text box which display the total.

<input name="txt_tot_amt" type="text" class="myinputlbl" id="txt_tot_amt" value="0.00" size="10" maxlength="40" onFocus="this.value=calcTOTchg()" />

function calcTOTchg()
{
var tot_val=0;
var tot_amt=0;

var txt_aa=parseFloat(document.getElementById('txt_a').value);
var txt_bb=parseFloat(document.getElementById('txt_b').value);
var txt_cc=parseFloat(document.getElementById('txt_c').value);
var txt_dd=parseFloat(document.getElementById('txt_d').value);
var txt_ee=parseFloat(document.getElementById('txt_e).value);
var txt_ff=parseFloat(document.getElementById('txt_f').value);
var txt_gg=parseFloat(document.getElementById('txt_g').value);
var txt_hh=parseFloat(document.getElementById('txt_h').value);
var txt_ii=parseFloat(document.getElementById('txt_i').value);

var tot_amt=(txt_aa)+(txt_bb)+(txt_cc)+(txt_dd)+(txt_ee)+(txt_ff)+(txt_dgg)+(txt_hh)+(txt_ii);

tot_val=parseFloat(tot_amt);

}
But there is another function that i used to calculate the value to the above text boxes.

function calcTot(qty,rate)
{
var tot=0;
tot=parseFloat(qty)*parseFloat(rate);
}
qty & rates entered by the user if he need.
Above all text boxes are getting values from that function when onFocus.
But some times user will enter values to there text boxes without calculating.
It means, User will enter directly to the text boxes.
When user on clicks the text box, it displays as NaN.
How can i avoid that???

Because I'm in big trouble

danguba
26 May 2011, 11:42 PM
You are not using ExtJS at all, aren't you?

Quick fix for solution you posted would be to check if fields you are reading contain any value and then adding them. You are getting NaN (Not a Number) because when field is empty value will be empty string and parseFloat('') = NaN. And of course NaN + anything = NaN

ishakya
27 May 2011, 1:42 AM
Thanks danguba,

is there any thing i can do to solve it?

danguba
27 May 2011, 1:59 AM
One solution would be to add
txt_aa= isNaN(txt_aa) ? 0 : txt_aa;
txt_bb= isNaN(txt_bb) ? 0 : txt_bb;
...
and so on

ishakya
27 May 2011, 2:07 AM
Thanks danguba,
Should i code it in a separate function or code it by opening a new script tag?