user2839151 user2839151 - 10 days ago 7
HTML Question

Javascript attributes for select option

I need some assistance with the code below, I want to set what has been chosen to the textarea but I dont want to equate/return the value attribute but the narration. for the case below the textarea is getting value 1 and I want it to get the narration Line One



var myOption = document.getElementById('priority1');
var myStrategy = document.getElementById('strategy1');
myStrategy.onchange = function () {
myOption.value = this.value;
}

<textarea id="priority1" name="priority1"></textarea>
<select id="strategy1" name="strategy1">
<option value=""></option>
<option value="1">Line one</option>
<option value="2">Line Two</option>
<option value="3">Line Three</option>
</select>




Answer

You need to find the selectedIndex's option's text content (or HTML Content).

<textarea id="priority1" name="priority1"></textarea>
<select id="strategy1" name="strategy1">
  <option value=""></option>
  <option value="1">Line one</option>
  <option value="2">Line Two</option>
  <option value="3">Line Three</option>
</select>
<script type="text/javascript">
  var myOption = document.getElementById('priority1');
  var myStrategy = document.getElementById('strategy1');
  myStrategy.onchange = function() {
    myOption.value = this.querySelectorAll('option')[this.selectedIndex].innerHTML;
  }
</script>