bitty installation snippets examples documentation q&a

snippets

copy text via a selector with messaging

This is the target text to copy with messaging
  • This is a basic copy button approach that provides an indication that the copy occurred with a brief update to the triggering button.
  • The b.quickCopy() method bakes in the same functionality if you don't need additional features.

html

<div id="targetDiv">This is the target text to copy with messaging</div>
<button data-s="copyText" data-r="restoreText">Copy Text</button>

javascript

export const b = {};

export async function copyText(ev, __, ___) {
  if (await b.copy("#targetDiv") === true) {
    ev.target.innerHTML = "Copied";
    b.debounce("textCopy", "restoreText", 1400);
  } else {
    ev.target.innerHTML = "Error: Text could not be copied";
    b.debounce("textCopy", "restoreText", 1400);
  }
}

export function restoreText(_, __, el) {
  el.innerHTML = "Copy Text";
}