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

Asked

Viewed 62 times

1

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

  • your question this too vague I will vote for closing

  • Could you provide more information to the question? I’m not getting to understand what you want.

  • 1

    @Gagel Gagel is new to the forum, in his opinion as would be the least vague way to ask what I asked?

  • @Gagel Gagel *perguntar

1 answer

2


Given such an HTML:

<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 objects to a variable like this:

var select = document.getElementById('meuSelect');
var input = document.querySelector('input');

then attach an event headphone to the select. You must listen to the event change and in the callback select will be the 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: https://jsfiddle.net/oz0z6Lcg/1

  • 2

    perfect thank you very much. For good connoisseur a drop is letter.

  • @Magichat great. If you want you can mark the question as accepted. And the same in the other question - you can mark as accepted one of the answers.

  • You mean click on the icon that looks like a teacher’s visa? Regarding the option to take the text inside the element, your solution brings all the options and tags too, how to get only the text of one of the options selected? What other question does it refer to?

  • 1

    @Magichat Sorry, had a bug. Fixed :)

  • Perfect, like Magik ;) Point for you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.