@istvzsig/clipboard
v1.0.1
Published
copy textcontent to clipboard
Downloads
15
Maintainers
Readme
Clipboard
This repository contains a JavaScript function that allows you to copy text from various HTML elements to the clipboard. It provides user feedback and handles errors gracefully.
Function Overview
The copyTextToClipboard
function takes a source HTML element as an argument and returns an asynchronous callback function. When invoked, this callback will copy the text from the specified element to the clipboard.
Function Signature
function copyTextToClipboard(sourceElement);
function blockClipboardEvents();
User Manual
Installation
npm install @istvzsig/clipboard
How to Use
- Set Up Your HTML: Create an input field or any other element containing the text you want to copy.
- Create a Button: Add a button that will trigger the copy action.
- Attach the Function: Use the copyTextToClipboard function to attach the copy functionality to the button click event.
Example HTML
<input type="text" id="textInput" value="Copy this text!" />
<button id="copyButton">Copy Text</button>
Example JavaScript
const copyButton = document.getElementById('copyButton');
const textInput = document.getElementById('textInput');
copyButton.addEventListener(
'pointerdown',
copyTextToClipboard(textInput)
);
Additional Notes
- Ensure that your web page is served over HTTPS, as the Clipboard API requires a secure context.
- The blockClipboardEvents function can be used to prevent default clipboard actions if needed.
Feel free to customize the content further based on your specific requirements!