Jyontex
asked on
How do you call a textbox dynamically
I am trying to set a value in a textbox based on a selection from a list box (by dynamically calling the field it references)
I am trying to do it using:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="Javascript">
function rule(Field){
<!--
Numb = document.formxx.Sel.value;
Val = Field+Numb;
document.formxx.Answer.val ue = Val.value;
//-->
}
</script>
</head>
<body>
<form name="formxx">
<select name="Sel" onChange="rule(document.fo rmxx.Test_ )">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="hidden" name="Test_1" value="answer1">
<input type="hidden" name="Test_2" value="answer2">
<br>
Answer = <input type="text" name="Answer">
</form>
</body>
This doesnt work, the problem appears to be the way i am building the reference to the fields. Can anyone help to solve this?
Thanx
</html>
I am trying to do it using:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="Javascript">
function rule(Field){
<!--
Numb = document.formxx.Sel.value;
Val = Field+Numb;
document.formxx.Answer.val
//-->
}
</script>
</head>
<body>
<form name="formxx">
<select name="Sel" onChange="rule(document.fo
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="hidden" name="Test_1" value="answer1">
<input type="hidden" name="Test_2" value="answer2">
<br>
Answer = <input type="text" name="Answer">
</form>
</body>
This doesnt work, the problem appears to be the way i am building the reference to the fields. Can anyone help to solve this?
Thanx
</html>
ASKER
sajuks i need the value of the field answer to be set to that of the field Text_[this.options[this.se lectedInde x].value] .
And i need the options to be numeric....and different from the field name its referencing.
i.e. if option 1 selected, then the value in answer should be "answer1"
Can you help?
Thanx
And i need the options to be numeric....and different from the field name its referencing.
i.e. if option 1 selected, then the value in answer should be "answer1"
Can you help?
Thanx
Try this one:
<script type="text/javascript">
function setAnswer(selectbox)
{
sval = selectbox.options[selectbo x.selected Index].val ue;
status = sval;
field = eval("selectbox.form.text_ " + sval);
if(field.value)
selectbox.form.answer.valu e = field.value;
else
selectbox.form.answer.valu e = "error";
}
</script>
<form>
<select onclick="setAnswer(this);" >
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="text" name="text_1" value="text1" />
<input type="text" name="text_2" value="text2" />
<input type="text" name="answer" value="" />
</form>
<script type="text/javascript">
function setAnswer(selectbox)
{
sval = selectbox.options[selectbo
status = sval;
field = eval("selectbox.form.text_
if(field.value)
selectbox.form.answer.valu
else
selectbox.form.answer.valu
}
</script>
<form>
<select onclick="setAnswer(this);"
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="text" name="text_1" value="text1" />
<input type="text" name="text_2" value="text2" />
<input type="text" name="answer" value="" />
</form>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
//do you want to append "answer" to the value that you're selecting ?
//then just do this
<script language="Javascript">
function rule(sval){
<!--
document.formxx.Answer.val ue = "answer"+sval
//-->
}
</script>
</head>
<body>
<form name="formxx">
<select name="Sel" onChange="rule(this.option s[ this.selectedIndex ].text)">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="hidden" name="Test_1" value="answer1">
<input type="hidden" name="Test_2" value="answer2">
<br>
Answer = <input type="text" name="Answer">
</form>
</body>
//then just do this
<script language="Javascript">
function rule(sval){
<!--
document.formxx.Answer.val
//-->
}
</script>
</head>
<body>
<form name="formxx">
<select name="Sel" onChange="rule(this.option
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="hidden" name="Test_1" value="answer1">
<input type="hidden" name="Test_2" value="answer2">
<br>
Answer = <input type="text" name="Answer">
</form>
</body>
ASKER
Thankyou robotman757 ...this makes sense, and does exactly what i wanted.
No problem..I had not even thought of trying something like that at first. I see how it can be useful though...
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="Javascript">
function rule(sval){
<!--
document.formxx.Answer.val
//-->
}
</script>
</head>
<body>
<form name="formxx">
<select name="Sel" onChange="rule(this.option
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="hidden" name="Test_1" value="answer1">
<input type="hidden" name="Test_2" value="answer2">
<br>
Answer = <input type="text" name="Answer">
</form>
</body>