xxxxxxxxxx
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);
}
xxxxxxxxxx
function copy(value) {
navigator.clipboard.writeText(value);
};
copy("Hello World");
xxxxxxxxxx
<html>
<input type="text" value="Hello world"(Can be of your choice) id="myInput"(id is the name of the text, you can change it later)
<button onclick="Hello()">Copy Text</button>
<script>
function Hello() {
var copyText = document.getElementById('myInput')
copyText.select();
document.execCommand('copy')
console.log('Copied Text')
}
</script>
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
function copyToClipboard(text) {
var copyText = document.createElement("textarea");
copyText.value = text;
document.body.appendChild(copyText);
copyText.select();
document.execCommand("copy");
document.body.removeChild(copyText);
}
// Usage example:
var textToCopy = "Hello, world!";
copyToClipboard(textToCopy);
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);
}
xxxxxxxxxx
var copyTextarea = document.getElementById("someTextAreaToCopy");
copyTextarea.select(); //select the text area
document.execCommand("copy"); //copy to clipboard