my Question is not a duplicate, I'm not using Jquery, I'm not using a textarea. I want to copy the value of a variable into the Clipboard using vanilla javascript.
I'd like to copy a String of Text that is stored in a variable into the Clipboard using the onClick Eventlistener off a Button.
I tried modifiyng this example, which uses a input field, but it's not working. I'm only beginning to understand Javascript, so please be kind. Also I'm looking for a solution that doesn't use a library.
function myFunction() {
var copyText = "This is a Test"
copyText.select();
document.execCommand("copy");
}
<button onclick="myFunction()">Copy text</button>