Skip to content

Instantly share code, notes, and snippets.

@JamesMGreene
Forked from hallvors/clipboard.js
Last active March 10, 2019 17:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesMGreene/8597596 to your computer and use it in GitHub Desktop.
Save JamesMGreene/8597596 to your computer and use it in GitHub Desktop.
var btn = document.getElementById("copy-button");
btn.addEventListener("click", clickHandler, false);
function clickHandler(e) {
var clip = new ClipboardEvent("copy");
clip.clipboardData.setData("text/plain", "foo");
clip.clipboardData.setData("text/html", "<b>foo</b>");
// CRITICAL: Must call `preventDefault();` to get this data into the system/desktop clipboard!!!
clip.preventDefault();
e.target.dispatchEvent(clip);
}
@hallvors
Copy link

fix clip.setData to clip.clipboardData.setData() :-)

@JamesMGreene
Copy link
Author

Good catch, thanks! Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment