Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!