5+ Effective Ways to Copy Text in JavaScript: From execCommand to Clipboard API

Sh Raj - Jun 11 '23 - - Dev Community

Copying text using JavaScript functions can be achieved through various methods. In this article, we will explore four different ways to copy text programmatically.

  1. document.execCommand('copy') Method: The document.execCommand('copy') method is a straightforward and widely supported way to copy text to the clipboard. Here's an example:
function copyText(text) {
  var tempInput = document.createElement('input');
  document.body.appendChild(tempInput);
  tempInput.value = text;
  tempInput.select();
  document.execCommand('copy');
  document.body.removeChild(tempInput);
}
Enter fullscreen mode Exit fullscreen mode

In this approach, we create a temporary <input> element, set its value to the desired text, select the text, execute the copy command, and finally remove the temporary element from the DOM.

  1. ZeroClipboard Library: ZeroClipboard is a popular JavaScript library that simplifies copying text to the clipboard. It provides a simple API and fallback mechanisms for older browsers. Here's an example:
function copyText(text) {
  var clipboard = new ZeroClipboard();
  clipboard.on('ready', function(event) {
    clipboard.setData('text/plain', text);
    console.log('Text copied successfully');
  });
  clipboard.on('error', function(event) {
    console.error('Failed to copy text: ', event.message);
  });
}
Enter fullscreen mode Exit fullscreen mode

In this approach, we instantiate a ZeroClipboard object, listen for the 'ready' event, set the data to be copied using clipboard.setData(), and handle any errors that may occur.

  1. Clipboard.js Library: Clipboard.js is a lightweight JavaScript library that simplifies copying text to the clipboard. It offers automatic fallback for older browsers and provides a clean and easy-to-use API. Here's an example:
function copyText(elementId) {
  var clipboard = new ClipboardJS('#' + elementId);
  clipboard.on('success', function(event) {
    console.log('Text copied successfully');
    clipboard.destroy();
  });
  clipboard.on('error', function(event) {
    console.error('Failed to copy text: ', event.action);
    clipboard.destroy();
  });
}
Enter fullscreen mode Exit fullscreen mode

In this approach, we create a ClipboardJS instance by passing the selector of the element to be copied, listen for the 'success' and 'error' events, and handle the respective outcomes.

These methods provide different options for copying text using JavaScript functions. Whether you prefer the simplicity of document.execCommand('copy'), the convenience of ZeroClipboard, or the lightweight nature of Clipboard.js, you can choose the method that best suits your needs.

Certainly! Here are two additional ways to copy text using JavaScript functions:

  1. Using the navigator.clipboard.writeText() Method: The Clipboard API provides a modern and asynchronous way to copy text. The navigator.clipboard.writeText() method allows us to write text directly to the clipboard. Here's an example:
function copyText(text) {
  navigator.clipboard.writeText(text)
    .then(function() {
      console.log('Text copied successfully');
    })
    .catch(function(error) {
      console.error('Failed to copy text: ', error);
    });
}
Enter fullscreen mode Exit fullscreen mode

In this approach, we use the navigator.clipboard.writeText() method to write the text to the clipboard. It returns a promise that resolves when the text is successfully copied and rejects if an error occurs.

  1. Select and Copy Method: Another approach to copying text is by selecting the text programmatically and using the document.execCommand('copy') method. Here's an example:
function copyText(elementId) {
  var element = document.getElementById(elementId);
  element.select();
  document.execCommand('copy');
  console.log('Text copied successfully');
}
Enter fullscreen mode Exit fullscreen mode

In this approach, we select the text by targeting the element containing the text, such as an <input> or <textarea> element, and then execute the copy command.

By utilizing these two additional methods, developers have a wider range of options to copy text in JavaScript. Whether you prefer the modern Clipboard API or the traditional select and copy method, you can choose the approach that best fits your specific requirements. Happy coding! Happy copying!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .