Adding Copy Buttons with Bitty

July 2026

Hitting a Single

Bitty has a .quickCopy() method. It does two things:

  1. Copies the content of the target element.

  2. Updates the text of the Copy button to say "Copied" for two seconds then reverts it.

For example:

content.html
<bitty-9 data-connect="/blog/01/n6/6r/q5/examples/example-1/bit.js"></bitty-9>

<button data-s="basicCopy">Copy</button>
<span data-r="basicCopy">
  The quick brown fox
</span>
bit.js
export const b = {};

export function basicCopy(_, sender, el) {
  b.quickCopy(el, sender);
}
Output

The quick brown fox

Connecting Keys

You can put multiple buttons on a page that all use the same copy function with the help of a data-key attribute:

content.html
<bitty-9 data-connect="/blog/01/n6/6r/q5/examples/example-2/bit.js"></bitty-9>

<div>
  <button data-s="copyWithKey" data-key="alfa">
    Copy
  </button>
  <span data-r="copyWithKey" data-key="alfa">
    The quick brown fox
  </span>
</div>

<div>
  <button data-s="copyWithKey" data-key="bravo">
    Copy
  </button>
  <span data-r="copyWithKey" data-key="bravo">
    The lazy dog
  </span>
</div>
bit.js
export const b = {};

export function copyWithKey(_, sender, el) {
  if (sender.prop("key") === el.prop("key")) {
    b.quickCopy(el, sender);
  }
}
Output
The quick brown fox
The lazy dog

How It Works

Outro

Most of bitty is about setting up functions to add your own features. The .quickCopy() method is an exception. It's fully baked. I found myself adding the functionality so often it made since to include it directly. Check it out if your site has content you want to make it easier for your visitors to grab.

-a

References

  • Bitty is a signals based framework rolled into a web component. You can find out more about it at bittyjs.com.

Endnotes

  • This example is using bitty-9. At the time of this writing, that version is a private beta. The current production version of bitty is bitty-8. It provides the same functionality.

id = "01/n6/6r/q5"