Link to home
Start Free TrialLog in
Avatar of Jyontex
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.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>
Avatar of sajuks
sajuks

<!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>
Avatar of Jyontex

ASKER

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
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>
ASKER CERTIFIED SOLUTION
Avatar of robotman757
robotman757

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
//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>
Avatar of Jyontex

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...