After Clicked the button then paste it into NotePad or anywhere else for testing.
<input type="text" id="copy" placeholder="write something...">
<button onclick="copyText()">Copy</button>
function copyText() {
let inputvalue = document.getElementById("copy");
inputvalue.select();
// select method can only use html element, not available on strings or plain values.
console.log(inputvalue);
navigator.clipboard.writeText(inputvalue.value);
}
const inputvalue=document.querySelector("#copy");
inputvalue.select();
inputvalue.setSelectionRange(0,99999);
try{
if(document.execCommand('copy')){
alert("Text is copied")
}
else{
alert("Failed to copy");
}
}
catch(err){
console.log("Failed "+err);
}
window.getSelection().removeAllRanges();