Skip to content Skip to sidebar Skip to footer

How To Use Removeeventlistener For Clipboarddata?

Page had the button which copies text to clipboard with code: export class ClipboardService { static copyToClipboard(toCopy: string) : void { document.addEventListener(

Solution 1:

The problem is that you're trying to remove the function by writing it again. In JS, two function, even with exactly the same code, are still different functions.

So you need to have a reference to the function to be able to use it in both calls:

exportclassClipboardService {
staticcopyToClipboard(toCopy: string) : void {
    constcreate_copy = (e : ClipboardEvent) => {
        e.clipboardData.setData('text/plain', toCopy);
        e.preventDefault();
    };
    document.addEventListener('copy', create_copy );
    document.execCommand('copy');
    document.removeEventListener('copy', create_copy );
}

}

Post a Comment for "How To Use Removeeventlistener For Clipboarddata?"