My website's url
is http://localhost:4200/reader-main
. I want that to be copied when an anchor
link is clicked. That copied link can be pasted anywhere.
Code:
HTML:
<div class="row permalink">
<a id="copyPermalink" class="copyLink" href="" (click)="copyToClipboard($event)" aria-label="Copy Permalink">Copy Permalink</a>
</div>
Javascript:
copyToClipboard(event: any): void {
const copyUrl = document.querySelector('copyLink');
window.getSelection();
try {
const successful = document.execCommand('copy');
const message = successful ? 'Successful' : 'Unsuccessful';
console.log(copyUrl);
alert(message);
} catch (err) {
console.log('Not able to copy', err);
}
return false;
}
But in above code, console.log(copyurl)
returns null. And URL isn't being copied. How to fix that?