ClipX.js is a tiny, dependency-free JavaScript library for clipboard management, text formatting and user feedback. Copy, cut and paste with plain HTML data- attributes, optionally format the text first, and show built-in toast notifications that you can fully customize.
Website: clipx.js.org
- Features
- Installation
- Initializing ClipX.js
- Attaching ClipX Functionality
- Attributes Overview
- Attribute Examples
- Text Formatting
- JavaScript API
- Events
- Browser Support & Notes
- Upgrading from 1.2.x
- Demo / Get Started Code
- License
- Copy, cut and paste from elements or static text.
- Text formatting (
trim,uppercase,titlecase, …) plus your own custom formatters. - Built-in toast notifications with custom messages, position, duration and styles. No extra UI library needed.
- Event delegation: works on elements added after page load, and
attach()is safe to call more than once. - Clear error feedback: a missing target or empty text shows an error toast instead of failing silently.
- Events:
clipx:successandclipx:errorare fired on the clicked element. - Fallback copy using
execCommandwhen the Clipboard API is unavailable. - Safe by default: toast messages are inserted as plain text, so
data-clipx-msgcannot inject HTML.
npm i clipxjsUse only one CDN.
jsDelivr
<script src="https://cdn.jsdelivr.net/npm/clipxjs/dist/clipx.min.js"></script>OR unpkg
<script src="https://unpkg.com/clipxjs/dist/clipx.min.js"></script>var clipx = new ClipX(); // `new clipx()` also worksClipX is an alias of the clipx constructor. Using it avoids shadowing the constructor when you name your instance clipx.
With a bundler:
const ClipX = require("clipxjs"); // or: import ClipX from "clipxjs";
const clipx = new ClipX();Set msg to true to stop the clicked element's text from changing (for example to "Copied!").
clipx.disable.msg = false; // true = never change the element textclipx.disable.toast = false; // true = never show toaststoast styles apply to every toast. The optional error styles are added on top for error toasts.
clipx.setCustomStyles({
toast: "background-color: #333; color: #fff; padding: 12px; border-radius: 5px;",
error: "background-color: #7a1f1f;",
});.clipx-js-svg-icon {
display: none;
}attach() uses event delegation, so it also works for elements added to the page later.
Default selector (matches data-clipx-text, data-clipx-target and data-clipx-paste):
clipx.attach();Custom selectors (data attributes, classes, anything):
clipx.attach(".clipx, .copy-btn, .your-class");
clipx.attach("[data-clipx-text], [data-clipx-target]");Remove all listeners:
clipx.detach();| Attribute | Description |
|---|---|
data-clipx-target |
Selector of the element to copy (or cut) from. |
data-clipx-text |
Static text to copy. |
data-clipx-paste |
Selector of the element to paste into. |
data-clipx-cut |
Cut instead of copy: clears the target after copying. |
data-clipx-format |
Comma-separated formatters applied before copy or paste. |
data-clipx-msg |
Custom success message (default Copied! / Pasted!). |
data-clipx-toast |
Show only the toast; leave the element text unchanged. |
data-clipx-disable-msg |
Don't change the element text. |
data-clipx-disable-toast |
Don't show the toast. |
data-clipx-toast-position |
Toast position: top or bottom (default). |
data-clipx-duration |
Milliseconds the message stays visible (default 1500). |
If several source attributes are present, the priority is data-clipx-paste, then data-clipx-target, then data-clipx-text.
Copies the text of the target element. Inputs, textareas and selects use their value; other elements use their text content.
<button data-clipx-target="#text-to-copy">Copy Text</button>
<p id="text-to-copy">This is the text to be copied.</p>Copies a static text string.
<button data-clipx-text="Static text to copy">Copy Static Text</button>Reads the clipboard and puts the text into the target element (see notes for permission requirements).
<textarea id="paste-here"></textarea>
<button data-clipx-paste="#paste-here">Paste</button>Copies the text of the target element and then clears it. Only works together with data-clipx-target; static text has nothing to clear.
<textarea id="text-to-cut">This text will be cut.</textarea>
<button data-clipx-target="#text-to-cut" data-clipx-cut>Cut Text</button>Transforms the text before it is copied or pasted. See Text Formatting.
<input id="email" value=" Ada@Example.COM ">
<button data-clipx-target="#email" data-clipx-format="trim,lowercase">Copy Email</button>Shows a custom message on the element and in the toast.
<button data-clipx-text="Another static text" data-clipx-msg="Text copied successfully!">
Copy with Custom Message
</button>Shows the toast only. The element's own text stays unchanged.
<button data-clipx-text="Text to copy" data-clipx-toast>Copy with Toast Only</button>Shows the toast but doesn't change the element's text.
<button data-clipx-text="Text to copy" data-clipx-disable-msg>Copy, No Text Change</button>Copies without showing a toast. The element text still changes.
<button data-clipx-text="Text to copy" data-clipx-disable-toast>Copy without Toast</button>Silent copy: no toast and no text change.
<button data-clipx-text="Text to copy" data-clipx-disable-toast data-clipx-disable-msg>
Silent Copy
</button><button data-clipx-text="Text to copy" data-clipx-toast-position="top">Toast on Top</button>
<button data-clipx-text="Text to copy" data-clipx-toast-position="bottom">Toast on Bottom</button>Sets how long (in milliseconds) the toast and the element text message stay visible.
<button data-clipx-text="Text to copy" data-clipx-duration="3000">3 Seconds</button>
<button data-clipx-text="Text to copy" data-clipx-toast data-clipx-duration="3000">3 Seconds, Toast Only</button>
<button data-clipx-text="Text to copy" data-clipx-disable-toast data-clipx-duration="3000">3 Seconds, Text Only</button>Formatters in data-clipx-format run left to right, before the text is copied or pasted.
| Formatter | Effect |
|---|---|
trim |
Remove leading and trailing whitespace |
uppercase / lowercase |
Change case |
capitalize |
Uppercase the first letter only |
titlecase |
Capitalize Each Word |
collapse |
Replace runs of whitespace with a single space |
singleline |
Replace line breaks with a space |
striphtml |
Remove HTML tags |
<button data-clipx-target="#messy" data-clipx-format="trim,collapse,titlecase">Copy Clean</button>Custom formatters:
clipx.addFormatter("reverse", function (text) {
return text.split("").reverse().join("");
});
// <button data-clipx-text="abc" data-clipx-format="reverse">Unknown formatter names are skipped with a console warning.
You can use ClipX without any HTML attributes. Copy, cut and paste methods return Promises.
clipx.copyText("hello"); // copy a string
clipx.cutText("hello", element); // copy, then clear `element`
clipx.copyFromElement(element, "trim"); // optional format
clipx.cutFromElement(element, "trim");
clipx.pasteText(); // resolves with the clipboard text
clipx.pasteToElement(element, "uppercase"); // paste into an element
clipx.format(" hi ", "trim,uppercase"); // synchronous, returns "HI"
clipx.addFormatter(name, fn); // add a custom formatter
clipx.attach(selector); // bind (returns the instance)
clipx.detach(); // unbind
clipx.setCustomStyles({ toast, error }); // toast stylingExample:
clipx.copyText("Hello!")
.then(function () { console.log("Copied"); })
.catch(function (err) { console.error(err); });ClipX dispatches bubbling events from the clicked element:
document.addEventListener("clipx:success", function (e) {
console.log(e.target, e.detail.type); // "copy" | "cut" | "paste"
});
document.addEventListener("clipx:error", function (e) {
console.log(e.detail.error);
});- Works in modern browsers. Copy uses the async Clipboard API and falls back to
execCommand("copy")if it is unavailable or blocked. - The Clipboard API requires a secure context (HTTPS or
localhost). - Paste uses
navigator.clipboard.readText(), which needs HTTPS, a user click, and usually shows a browser permission prompt. Support and prompts vary between browsers; when unsupported, an error toast is shown. - Cutting from a non-input element sets its
textContentto an empty string, which removes any child elements inside it. - Static text (
data-clipx-text) can only be copied.
- The default success message is now
Copied!(the stray space is gone). attach()uses event delegation and can be called repeatedly; new elements are picked up automatically. Callingattach()with no argument uses the default selector.- Busy state is tracked per element. The old
isProcessingproperty was removed. setCustomStyles()accepts an optionalerrorkey.- Missing targets and empty text now show an error toast.
- New:
data-clipx-paste,data-clipx-format,addFormatter(),pasteText(),pasteToElement(),detach(), and theclipx:success/clipx:errorevents.
<h1>ClipX.js Demo</h1>
<button data-clipx-text="Static text to copy" data-clipx-msg="Static text copied!">
Copy Static Text
</button>
<p id="text-to-copy"> This text will be copied or cut. </p>
<button data-clipx-target="#text-to-copy" data-clipx-format="trim">Copy Text</button>
<button data-clipx-target="#text-to-copy" data-clipx-cut>Cut Text</button>
<textarea id="paste-here" placeholder="Paste here"></textarea>
<button data-clipx-paste="#paste-here">Paste</button>
<button data-clipx-text="Toast only, on top" data-clipx-toast data-clipx-toast-position="top">
Copy with Toast Only
</button>
<button data-clipx-text="Text with 5 seconds toast" data-clipx-toast data-clipx-duration="5000">
Copy with 5 Seconds Toast
</button>
<script src="https://cdn.jsdelivr.net/npm/clipxjs/dist/clipx.min.js"></script>
<script>
var clipx = new ClipX();
// Optional: style the toast notifications
clipx.setCustomStyles({
toast: "background-color: #333; color: #fff; padding: 12px; border-radius: 5px;",
});
// Bind all elements with data-clipx-text / -target / -paste
clipx.attach();
</script>MIT © 2024 Mr Akshay Shinde. See LICENSE.