Answers for "js copy clipboard"

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
11

js copy text to clipboard

function copy() {
  var copyText = document.querySelector("#input");
  copyText.select();
  document.execCommand("copy");
}

document.querySelector("#copy").addEventListener("click", copy);
Posted by: Guest on September-17-2019
5

copy to clipboard javascript

// Since Async Clipboard API is not supported for all browser!
function copyTextToClipboard(text) {
  var textArea = document.createElement("textarea");
  textArea.value = text
  document.body.appendChild(textArea);
  textArea.focus();
  textArea.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    console.log('Copying text command was ' + msg);
  } catch (err) {
    console.log('Oops, unable to copy');
  }

  document.body.removeChild(textArea);
}
Posted by: Guest on May-20-2020
2

javascript how-do-i-copy-to-the-clipboard-in-javascript

function copyToClipboard(text) {
  var input = document.body.appendChild(document.createElement("input"));
  input.value = text;
  input.focus();
  input.select();
  document.execCommand('copy');
  input.parentNode.removeChild(input);
}
Posted by: Guest on May-20-2020
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