Answers for "auto copy to clipboard javascript"

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
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
0

copy to clipboard javascript dom

navigator.clipboard.writeText('Text');
Posted by: Guest on February-02-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 "auto copy to clipboard javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language