Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
Copy text/data from drop down menu when user click button using javascript?

function myFunction() {
  var copyText = document.getElementById("1");
  var copyText = document.getElementById("2");
  copyText.select();

  navigator.clipboard.writeText(copyText.value);

  alert("Copied the text: " + copyText.value);
}
<select id="1">
  <option>Apple</option>
  <option>Carrot</option>
</select>
<select id="2">
  <option>Fruit</option>
  <option>Vegetable</option>
</select>

<button onclick="myFunction()">Copy text</button>

When Apple and Fruit is selected in the drop down, and click Copy text button, my clipboard will have "Apple/Fruit"

I can't figure it out. Please help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Let's start with what's wrong:

  1. you are reusing the same variable copyText for both elements, hens only the last element will be available in that variable.
  2. as the error shows, element select (which is delcared into copyText variable) doesn't have function select()
  3. you are trying copy to clipboard a value from a single element

So, to minimally fix your code you'll need declare two different variables for your elements, and combine values from both into one:

function myFunction() {
  var copyText1 = document.getElementById("1");
  var copyText2 = document.getElementById("2");

  var value = copyText1.value + "/" + copyText2.value;
  navigator.clipboard.writeText(value);

  alert("Copied the text: " + value);
}
<select id="1">
     <option>Apple</option>
     <option>Carrot</option>
</select>
<select id="2">
     <option>Fruit</option>
     <option>Vegetable</option>
</select>

<button onclick="myFunction()">Copy text</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda