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
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() {
var copyText = document.querySelector("#input");
copyText.select();
document.execCommand("copy");
}
document.querySelector("#copy").addEventListener("click", copy);
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
document.getElementById("cp_btn").addEventListener("click", copy_password);
function copy_password() {
var copyText = document.getElementById("pwd_spn");
var textArea = document.createElement("textarea");
textArea.value = copyText.textContent;
document.body.appendChild(textArea);
textArea.select();
document.execCommand("Copy");
textArea.remove();
}
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);