Skip to content

Instantly share code, notes, and snippets.

@bago2k4
Created January 29, 2019 22:16
Show Gist options
  • Save bago2k4/6df2887ad4db25b4588c8aabe8bd12c6 to your computer and use it in GitHub Desktop.
Save bago2k4/6df2887ad4db25b4588c8aabe8bd12c6 to your computer and use it in GitHub Desktop.
<html>
<head></head>
<body>
<button onclick="copyToClip(document.getElementById('foo').innerHTML)">
Copy the stuff
</button>
<div id=foo style="display:none">
This is some data that is not visible.
You can write some JS to generate this data.
It can contain rich stuff. <b> test </b> me <i> also </i>
<span style="font: 12px consolas; color: green;">Hello world</span>
You can use setData to put TWO COPIES into the same clipboard,
one that is plain and one that is rich.
That way your users can paste into either a
<table><tbody><tr><td>Table content</td></tr></tbody></table>
<style type="text/css">
p {
color: blue !important;
}
</style>
<ul>
<li>plain text editor</li>
<li>or into a rich text editor</li>
</ul>
</div>
<script type="text/javascript">
function copyToClip(str) {
function listener(e) {
e.clipboardData.setData("text/html", str);
e.clipboardData.setData("text/plain", str);
e.preventDefault();
}
document.addEventListener("copy", listener);
document.execCommand("copy");
document.removeEventListener("copy", listener);
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment