How to send the value of one of the options of a "select" to an input text?
How to send the value of one of the options of a "select" to an input text?
Given an HTML like this:
<select id="meuSelect">
<option value="a">Alfa</option>
<option value="b">Beta</option>
<option value="g">Gama</option>
</select>
<input type="text" />
You can save the objects to a variable like this:
var select = document.getElementById('meuSelect');
var input = document.querySelector('input');
then joins an event handset to select
. You should listen for the change
event and in the callback the select
will be this
. The rest is simple:
select.addEventListener('change', function() {
input.value = this.value;
// se quiseres o texto usa
// var option = this.children[this.selectedIndex];
// input.value = option.innerHTML;
});