Forum Moderators: open

Message Too Old, No Replies

dymanic value change in a textbox

text box manipulation

         

jbhavin

6:30 am on Jun 22, 2004 (gmt 0)

10+ Year Member



hello guys,

I am having mulitple textboxes, where numerals and fed and onBlur of each text box, the value gets added and is displayed in a text box at bottom...

Now, if i enter once and mov down the form , addition is alright...

But , if i move up again, and want to change the value of a textbox, the final total should also reflect the latest addition...

Also, if it reaches zero and then again ifi change, it should reflect latest changes

Help Please

natty

11:49 am on Jun 22, 2004 (gmt 0)

10+ Year Member



mbe..
<form ....
<inuput onblur="doTotals()" .... />
</form>

function doTotals(){
var theTotal,fld;
for (var x=0;x<document.forms[0].elements.length;x++){
fld = document.forms[0].elements[x];
if(fld.type == "text"){
if(!isNaN(fld.value)){
theTotal += fld.value;
}
}
}
document.forms[0].totalfieldname.value = theTotal;
}

that might work..

Bernard Marx

6:15 pm on Jun 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In natty's code, theTotal needs to be initialized.
I've tweaked a bit too (hope you don't mind, natty). Sending the form as an argument neatens things up.

[pre]
---- HTML ----
<form ....
<input onblur="doTotals(this.form)" .... />
</form>

---- JS ----

function doTotals(form){
var theTotal=0,fld;
for (var x=0;x<form.elements.length;x++){
fld = form.elements[x];
if(fld.type == "text" &&!isNaN(fld.value)){
theTotal += fld.value;
}
}
form.totalfieldname.value = theTotal;
}
[/pre]