const copyToClipboard = (text) => navigator.clipboard.writeText(text);
copyToClipboard("Hello World");
// Method 4 : Copying from input
<head>
<script>
function copyToCliBoard() {
var copyText = document.getElementById("myInput");
copyText.select();
copyText.setSelectionRange(0, 99999); /* For mobile devices */
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
}
</script>
</head>
<body>
<input type="text" value="Some Context" id="myInput">
<button onclick="copyToCliBoard()">Copy text</button>
</body>
navigator.clipboard.writeText('Text To Copy');
const copyToClipboard = (text) =>
navigator.clipboard?.writeText && navigator.clipboard.writeText(text);
// Testing
copyToClipboard("Hello World!");
const copyToClipboard = str => {
if (navigator && navigator.clipboard && navigator.clipboard.writeText)
return navigator.clipboard.writeText(str);
return Promise.reject('The Clipboard API is not available.');
};
const copyToClipboard = (text) => navigator.clipboard.writeText(text);
copyToClipboard("Hello World");
methods: {
async copyURL(mytext) {
try {
await navigator.clipboard.writeText(mytext);
alert('Copied');
} catch($e) {
alert('Cannot copy');
}
}
}
const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
const selected =
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
el.select();
document.execCommand('copy');
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
};
// Example
copyToClipboard('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.