Send one of the values of select and paste in an input text?

1

How to send the value of one of the options of a "select" to an input text?

    
asked by anonymous 13.05.2016 / 23:29

1 answer

2

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;
});

jsFiddle: link

    
13.05.2016 / 23:49