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.value = Val.value;
//-->
}
</script>
</head>

<body>
<form name="formxx">
<select name="Sel" onChange="rule(document.formxx.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>
JyontexAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sajuksCommented:
<!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(sval){
<!--

document.formxx.Answer.value = sval
//-->
}
</script>
</head>

<body>
<form name="formxx">
<select name="Sel" onChange="rule(this.options[this.selectedIndex].value)">
<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>
0
JyontexAuthor Commented:
sajuks i need the value of the field answer to be set to that of the field Text_[this.options[this.selectedIndex].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
0
RoonaanCommented:
Try this one:

<script type="text/javascript">
  function setAnswer(selectbox)
  {
    sval = selectbox.options[selectbox.selectedIndex].value;
    status = sval;
    field = eval("selectbox.form.text_" + sval);
    if(field.value)
      selectbox.form.answer.value = field.value;
    else
      selectbox.form.answer.value = "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>
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

robotman757Commented:
Try doing it this way. I changed it a little to make things clearer (to me at least). What I think you are trying to do is use a listbox or dropdown box to select the name of a control. Here goes:

<html>
<head>
<script language="Javascript">
function rule(Field){
<!--
var tmp1 = Field.name;
//alert(tmp1);
var Numb = document.formxx.Test.value;
var Val = tmp1+Numb;
var Val2 = document.getElementById(Val).value;

document.formxx.Answer.value = Val2;
//-->
}
</script>
</head>

<body>
<form name="formxx">
<select name="Test" onChange="rule(this)">
<option value="1">1</option>
<option value="2">2</option>
</select>


<input type="hidden" name="Test1" id="Test1" value="answer1">
<input type="hidden" name="Test2" id="Test2" value="answer2">
<br>
Answer = <input type="text" name="Answer">
</form>
</body>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sajuksCommented:
//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.value = "answer"+sval
//-->
}
</script>
</head>

<body>
<form name="formxx">

<select name="Sel" onChange="rule(this.options[ 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>
0
JyontexAuthor Commented:
Thankyou robotman757 ...this makes sense, and does exactly what i wanted.
0
robotman757Commented:
No problem..I had not even thought of trying something like that at first. I see how it can be useful though...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.