How can I show the text of selectbox to each textbox based on selected event? My code only shows the value of selectbox to textbox. I want to replace it with text of each option.
Here is my code:
var selectBox = document.getElementById('mySelect');
var selectBox2 = document.getElementById('mySelect2');
selectBox.addEventListener('change', handleChange);
selectBox2.addEventListener('change', handleChange);
function handleChange(event)
{
if (event.target.id == "mySelect") {
document.getElementById('myInput').value = selectBox.value;
} else {
document.getElementById('myInput2').value = selectBox2.value;
}
}
<select id="mySelect">
<option value="Option 1"> this Text</option>
<option value="Option 2">this Text 2</option>
</select>
<select id="mySelect2">
<option value="Option 1">this Text</option>
<option value="Option 2">this Text 2 </option>
</select>
<input type="text" id="myInput" />
<input type="text" id="myInput2" />