Answers for "how to copy to clipboard in js"

26

javascript text to clipboard

function copyToClipboard(text) {
   const elem = document.createElement('textarea');
   elem.value = text;
   document.body.appendChild(elem);
   elem.select();
   document.execCommand('copy');
   document.body.removeChild(elem);
}
Posted by: Guest on March-20-2020
2

js copy to clipboard

Also works on safari!
  
function copyToClipboard() {
    var copyText = document.getElementById("share-link");
    copyText.select();
    copyText.setSelectionRange(0, 99999);
    document.execCommand("copy");
}
Posted by: Guest on May-17-2021
0

copy to clipboard javascript dom

navigator.clipboard.writeText('Text');
Posted by: Guest on February-02-2021
2

js copy string to clipboard

const el = document.createElement('textarea');
el.value = str;	//str is your string to copy
document.body.appendChild(el);
el.select();
document.execCommand('copy');	// Copy command
document.body.removeChild(el);
Posted by: Guest on August-22-2020
1

js copy to clipboard

function textToClipboard (text) {
    var dummy = document.createElement("textarea");
    document.body.appendChild(dummy);
    dummy.value = text;
    dummy.select();
    document.execCommand("copy");
    document.body.removeChild(dummy);
}
Posted by: Guest on February-28-2021
0

javascript copy to clipboard

const copyToClipboard = () => {

  navigator.permissions.query({name: "clipboard-write"}).then(result => {
    if (result.state == "granted" || result.state == "prompt") {
      // write to the clipboard now
      updateClipboard('I copy this string');
    }
  });
};

const updateClipboard = (newClip) => {
  
  navigator.clipboard.writeText(newClip).then(() => {
    // clipboard successfully set
	console.log('success');
  }, () => {
    // clipboard write failed
    console.log('Failed to copy');
  });
};

const btn = document.getElementById('copy-button');

btn.addEventListener('click', copyHashtagToClipboard);
Posted by: Guest on February-28-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language