Click to See Complete Forum and Search --> : Dynamic Changes of a Textfield


janardhanreddy
01-03-2007, 03:51 AM
Hello,

i am having a form in which i have to change sm textfield's value based on above enterd field.

let me give 1 example --


1 textbox --- for Net Sales (entrd by user)
2nd for -------- Tax ( again entrd by user)

3rd one for ------ Gross Sales ( i want this field to gt automatically entered based on above textboexs ie. NET_SALES - TAX = GROSS_SALES)

do u have any idea ????

thanks for reading.....

-janardan

ricp
01-03-2007, 04:27 AM
Do you wish to have a button that sets the values? Or do you want them populated once the user changes the value in the input? Either are pretty straight forward.

disgracian
01-03-2007, 06:22 AM
<html>
<head>
<title>Form test</title>
<script type="text/javascript">
var inputArray=new Object();
inputArray["textfield1"]="";
inputArray["textfield2"]="";
var digit=/^\d*\.?\d+$/;

function addValue(input){
var i=0;
for(x in inputArray){
if(input.id==x)inputArray[x]=input.value;
if(inputArray[x] && digit.test(inputArray[x]))i++;
}
if(i==2){
var textStr="$"+inputArray["textfield1"]+" - $"+inputArray["textfield2"]+
" = $"+(inputArray["textfield1"]-inputArray["textfield2"]);
document.getElementById("textbox").value=textStr;
}
}
</script>
</head>
<body>
<h1>Here is a form</h1>
<form id="niceform">
<label for="textfield1">Text Field 1</label>
<input id="textfield1" type="text" onchange="addValue(this);"></input><br>
<label for="textfield2">Text Field 2</label>
<input id="textfield2" type="text" onchange="addValue(this);"></input>
<br><label for="textbox">A big text box</label><br>
<textarea id="textbox" rows="3" cols="30"></textarea>
</form>
</body>
</html>Copy and paste this and tell me if it does what you want.

Cheers,
D.