I'm trying to send a text using js function and copy it to clipboard, but somehow can't find a practical solution online. All I found are about copying from an element.
Is it possible to do it? I'll be really thankful if you help.
<a onClick="CopyToClipboard('{{ product.link }}')"><i class="fa fa-copy">Copy</i></a>
function CopyToClipboard(link) {
link.select();
document.execCommand("Copy");
alert("Copied the text: " + link);
}
I also tried this:
function CopyToClipboard(link) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(link);
range.select().createTextRange();
document.execCommand("copy");
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(link);
window.getSelection().addRange(range);
document.execCommand("copy");
alert("text copied")
}}