Pass multiple data to an input, via a select

Asked

Viewed 373 times

1

I’m trying to get past several data for a input, I had almost no idea it would be a textarea for the amount but it would be another select ? This way:

Imagem com 1 somente

I’m trying to keep it that way:

inserir a descrição da imagem aqui

If anyone can give a hint I will be grateful. Follow the code:

<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"/> 

1 answer

1


I don’t know if it’s the best implementation, but you can solve your problem by changing your 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
   }    
};

Follows jsfiddle :)

And to pass the option selected, you can do just like 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
};

Follows the jsfiddle :)

  • hum saw that this adding all as I can one by one selected one at a time or could do in php?

  • to pass one at a time you can do so jsfiddle i will update in post :) the above solution is with javascript, php already flees from my area of knowledge hehe

  • 1

    mano vc helped me a lot was doing in php but give up, it even worked but it looks better for me so less code thank you even. blessed be it. I will adapt to another cosia now kkk

Browser other questions tagged

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