Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Javascript - copy text string on click
<!DOCTYPE html>
<div>other text</div>
// How do I get a hold of clipboardData without a copy event?
// As in from another event handler, like a click event on a separate element?
// The copy event is not firing. – Chloe May 12 '18 at 20:21
const dt = new DataTransfer();
const span = document.querySelector("span");
const button = document.querySelector("button");
span.addEventListener("click", e => {
button.addEventListener("click", e => {
console.log(dt.getData("text"), dt.items);
document.addEventListener("copy", e => {
dt.setData("text", e.clipboardData.getData("text"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment