Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
Why is my multi line copy code copying single line?

Here is my js:

try{
      
let input = document.createElement("input")
      input.style="display:none; white-space: pre-wrap"
    input.value = message
    document.body.appendChild(input)
    var copyText = input

  /* Select the text field */
  copyText.select();
  copyText.setSelectionRange(0, 99999); /* For mobile devices */

  /* Copy the text inside the text field */
  navigator.clipboard.writeText(copyText.value);
  
  /* Alert the copied text */
  alert("Copied the text: " + copyText.value);
      document.body.innerHTML+=copyText.value
  } catch(e){console.log(e)}

I think it should work, but it only copies single-line strings. When I try to copy a multi-line string, such as

let message = `Hello
What is your name?
`

I paste it as

`Hello What is your name?`

Why is it not allowing multi-line strings?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The value of an input can't have newlines in it. If you assign a string containing newlines to an input's value, the newlines get stripped out:

const input = document.createElement("input");
input.type = "text";
const original = "one\ntwo";
input.value = original;
console.log("original: " + JSON.stringify(original));
console.log("value:    " + JSON.stringify(input.value));

Instead of passing it through an input, just pass message directly to writeText. (Or if there's some particular reason for passing it through an element first, use a textarea instead of an input.)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda