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>Cuando se selecciona Apple y Fruit en el menú desplegable y se hace clic en el botón Copiar texto, mi portapapeles tendrá "Apple/Fruit"
No puedo resolverlo. Por favor ayuda.
Comencemos con lo que está mal:
copyText para ambos elementos, entonces solo el último elemento estará disponible en esa variable.select de elementos (que está definida en la variable copyText ) no tiene la función select()Entonces, para corregir mínimamente su código, necesitará declarar dos variables diferentes para sus elementos y combinar valores de ambos en uno:
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>