How to display Drop down list value in a text field using JavaScript?

If you want to display the value selected in a drop down list in a text field using JavaScript, you can use the following code:

<select id="mySelect" onchange="displaySelected()">
  <option value="value1">Value 1</option>
  <option value="value2">Value 2</option>
  <option value="value3">Value 3</option>
</select>

<input type="text" id="myText">

<script>
function displaySelected() {
  var selectedValue = document.getElementById("mySelect").value;
  document.getElementById("myText").value = selectedValue;
}
</script>

In this code, we first create a drop down list with options and an empty text field. We then add an onchange event to the drop down list that calls the displaySelected() function when an option is selected.

The displaySelected() function gets the value of the selected option using the getElementById() method and sets the value of the text field using the value property.

By using this code, you can easily display the selected value from a drop down list in a text field using JavaScript.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information