0

I'm trying to figure out, how to copy string and paste from JavaScript.

This code example:

function copyLink() {
  var copyText = document.getElementById("myInput");
  copyText.select();
  copyText.setSelectionRange(0, 99999);
  document.execCommand("copy");
} 

Copies from myInput value:

  <input type="text" value="ttps://site.org/" id="myInput">

but I'm trying copy text from variable, not included to html value:

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

which is in .js to allow user copy/paste:

function copyText() {
   var text = "long text...";
   ...
} 

It looks simple, but seems like I'm searching incorrectly, because I can't find the method.

4
  • 1
    var copyText = document.getElementById("myInput").value might do the trick Commented Sep 1, 2020 at 21:32
  • You can also use the Clipboard API. Commented Sep 1, 2020 at 21:35
  • @user3791775 Hello, you mean adding to myInput, can you provide some example Commented Sep 1, 2020 at 21:51
  • document.getElementById("myInput") returns the html, document.getElementById("myInput");.value returns the value of the input. I'm not sure the rest of your code works (the select and setSelectionRange part) Commented Sep 1, 2020 at 21:54

3 Answers 3

1

The best way is to create a Dummy element, copy the content and remove it from the dom.

// Create a dummy input
var dummy = document.createElement("input");

// Inject the content
dummy.value=copyText;

// Add it to the dom
document.body.appendChild(dummy);

// Select it
dummy.select();

// Copy the content
document.execCommand("copy");

// Clean the dom
document.body.removeChild(dummy);
Sign up to request clarification or add additional context in comments.

Comments

1

As a quick fix, you could just stick the value you want to copy into an input field (even a hidden one), and copy the same way.

Here's a Codepen example: https://codepen.io/kshetline/pen/ExKwjjq?editors=1111

function copyText() {
  document.getElementById('hidden').value = new Date().toLocaleString();
  var copyText = document.getElementById('hidden');
  copyText.select();
  document.execCommand('copy');  
}
<input id="hidden" type="text" style="opacity: 0; position: absolute; top: -5em">
<button type="button" onclick="copyText()">Copy</button><br>
<label for="paste">Paste here: <input id="paste" type="text"></label>

There's a more advanced API for clipboard operations, however, that's described in this article: https://web.dev/async-clipboard/

2 Comments

Hello, if I get you correct, I have to hide myInput document.getElementById("myInput").style.display = "none"; but this way it does not make copy
I'd hide it using an opacity of 0 instead.
0

You can use the Async Clipboard API:

async function copy() {
  try {
    await navigator.clipboard.writeText(input.value);
    console.log('Value copied to clipboard');
  } catch (err) {
    console.error('Failed to copy: ', err);
  }
}

For more details, please see this article.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.