0

How can I add the "variable value" to my text area based on the users selection in the drop-down box above.

Here is the code that II have so far:

<html>
<head>

<body>

<select id="dropdown">
    <option value="">None</option>
    <option value="textArray1">text1</option>
    <option value="textArray2">text2</option>
    <option value="textArray3">text3</option>
    <option value="textArray4">text4</option>
</select>

<br /><br />

<textarea id="mytext"></textarea>

<script type="text/javascript">

var textArray1 = 'this is going to be a long sting of text for text 1 value';
var textArray2 = 'this is going to be a long sting of text for text 2 value';
var textArray3 = 'this is going to be a long sting of text for text 3 value';
var textArray4 = 'this is going to be a long sting of text for text 4 value';

var mytextbox = document.getElementById('mytext');
var mydropdown = document.getElementById('dropdown');

mydropdown.onchange = function(){
      mytextbox.value = mytextbox.value  + this.value; //to appened
     //mytextbox.innerHTML = this.value;
}
</script>

</body>
</html>
Blnukem
  • 163
  • 1
  • 4
  • 12

1 Answers1

0

Try making this change in your onchange function:

mytextbox.value = mytextbox.value  + window[this.value]; //to appened

You could've also used eval() on the this.value, but eval() is about as popular as a red-headed step child. See this question for more info on the differences.

jsFiddle example

Community
  • 1
  • 1
j08691
  • 204,283
  • 31
  • 260
  • 272