xxxxxxxxxx
var copyTextarea = document.getElementById("someTextAreaToCopy");
copyTextarea.select(); //select the text area
document.execCommand("copy"); //copy to clipboard
xxxxxxxxxx
function copyToClipboard(text) {
const elem = document.createElement('textarea');
elem.value = text;
document.body.appendChild(elem);
elem.select();
document.execCommand('copy');
document.body.removeChild(elem);
}
xxxxxxxxxx
function copy(value) {
navigator.clipboard.writeText(value);
};
copy("Hello World");
xxxxxxxxxx
function copy() {
var copyText = document.querySelector("#input");
copyText.select();
document.execCommand("copy");
}
document.querySelector("#copy").addEventListener("click", copy);
xxxxxxxxxx
var el = x;
try {
el.select();
el.setSelectionRange(0, 99999); /* For mobile devices */
document.execCommand("copy");
} catch {
navigator.clipboard.writeText(el.innerText);
}
xxxxxxxxxx
async function copyToClipboard(code) {
if (navigator.clipboard?.writeText) {
try {
await navigator.clipboard.writeText(code);
} catch(e) {
console.error('Error while copying code', e);
}
}
}
copyToClipboard("add string here...");
xxxxxxxxxx
navigator.clipboard
.writeText("Text")
.then(() => console.log("Copied to clipboard"))
.catch((err) => console.log(err))
xxxxxxxxxx
function textToClipboard (text) {
var dummy = document.createElement("textarea");
document.body.appendChild(dummy);
dummy.value = text;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
}