Javascript Function Copy [text] to Clipboard
Javascript Function Copy [text] to Clipboard Works All Browsers. To Copy a Div Contents to Clipboard Simply Call copyToClipboard(div.innerHTML) or copyToClipboard(div.textContent)
Home
Short:
/* -------------------------------------- function to copy [text] to clipboard ----------------------------------- */ function copyToClipboard(text) { navigator.clipboard.writeText(text) .then(() => { console.log('Text copied to clipboard successfully'); }) .catch(err => { console.error('Error copying text to clipboard:', err); }); } // Example usage var textToCopy = "This text will be copied to clipboard."; copyToClipboard(textToCopy);
source code home