Clipboard API - programmatically manipulating the clipboard

The Clipboard API finally allows developers to copy to a user's clipboard without those nasty Adobe Flash plugin hacks that were not cross-browser/cross-device-friendly. Here's how you'll copy a selection to a user's clipboard:

<script>function copy2Clipboard(text) {  const textarea = document.createElement('textarea');  textarea.value = text;  document.body.appendChild(textarea);  textarea.focus();  textarea.setSelectionRange(0, text.length);  document.execCommand('copy');  document.body.removeChild(textarea);}</script><button onclick="copy2Clipboard('Something good!')">Click me!</button>

First of all, we need the user to actually click the button. Once the user clicks the button, we call ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.