Pass several data to an input, using a select

1
I'm trying to pass several data to an input, I had almost no idea that it would be a textarea by quantity but would be another select ? Here's how:

I'mtryingtomakeitlooklikethis:

Ifanyonecangivemeatip,I'llbegrateful.Hereisthecode:

<script type="text/javascript"> 
function passar(){ 
var valorA = document.getElementById("valorA"); 
var nome = document.getElementById("nome"); 
nome.value = valorA.value; 
}; 
</script>
<select name="valorA" id="valorA" size="3" multiple>
  <option value="Gezer">Gezer</option> 
  <option value="João" selected>João</option>
  <option value="Marcos">Marcos</option>
</select>
 
<button type="button" onclick="passar();"> passar valores </button> 

Nome:<input type="text" id="nome" size="10"/> 
    
asked by anonymous 03.11.2015 / 00:16

1 answer

1

I do not know if it's the best implementation, but you can solve your problem by changing its function like this:

function passar() {
   var valorA = document.getElementById("valorA");
   var nome = document.getElementById("nome");

   for(var i = 0; i <= valorA.options.length; i++) { //itero em cada option
     nome.value += (valorA.options[i].value + '\n'); //seto o (value|text) no textarea com uma quebra de linha
   }    
};

Follow jsfiddle :)

And to pass the option selected, you can simply do this:

function passar() {
    var valorA = document.getElementById("valorA");
    var nome = document.getElementById("nome");

    nome.value += (valorA.value + '\n'); //Acessa diretamente o value do select que já é o elemento selecionado
};

Follow the jsfiddle :)

    
03.11.2015 / 01:39