Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do you call a textbox dynamically

Posted on 2004-11-18
7
Medium Priority
?
238 Views
Last Modified: 2012-06-27
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>
0
Comment
Question by:Jyontex
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 33

Expert Comment

by:sajuks
ID: 12613452
<!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
 

Author Comment

by:Jyontex
ID: 12613536
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
 
LVL 49

Expert Comment

by:Roonaan
ID: 12613568
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
robotman757 earned 300 total points
ID: 12613571
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
 
LVL 33

Expert Comment

by:sajuks
ID: 12613572
//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
 

Author Comment

by:Jyontex
ID: 12613726
Thankyou robotman757 ...this makes sense, and does exactly what i wanted.
0
 
LVL 3

Expert Comment

by:robotman757
ID: 12614893
No problem..I had not even thought of trying something like that at first. I see how it can be useful though...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question