xxxxxxxxxx
var dummyContent = "this is to be copied to clipboard";
var dummy = $('<input>').val(dummyContent).appendTo('body').select()
document.execCommand('copy')
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
<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
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
var copyTextarea = document.getElementById("someTextAreaToCopy");
copyTextarea.select(); //select the text area
document.execCommand("copy"); //copy to clipboard
xxxxxxxxxx
$(document).ready(function(){
$("#ewefmwefmp").click(function(){
//alert()
var copyText = document.getElementById("myInput");
copyText.select();
copyText.setSelectionRange(0, 99999)
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
});
});