Clipboard API in JavaScript

Clipboard API in JavaScript

The Clipboard API in JavaScript provides a convenient way to copy content to the user's clipboard, enabling seamless interaction with the clipboard functionality of the operating system. This feature is particularly useful in web applications where users want to copy content such as text, URLs, or data tables.

Key Methods of the Clipboard API:

  1. navigator.clipboard.writeText(text): This method writes the specified text to the clipboard.
  2. navigator.clipboard.readText(): This method reads the text currently stored in the clipboard.

Example: Copying Text to a Clipboard

Let's create a simple example demonstrating how to copy text to the clipboard using the Clipboard API:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Clipboard API Example</title>
</head>
<body>
    <button id="copyButton">Copy Text</button>
    <script>
        document.getElementById('copyButton').addEventListener('click', async () => {
            try {
                const textToCopy = 'Hello, Clipboard!';
                await navigator.clipboard.writeText(textToCopy);
                console.log('Text copied to clipboard:', textToCopy);
                alert('Text copied to clipboard: ' + textToCopy);
            } catch (err) {
                console.error('Failed to copy text: ', err);
            }
        });
    </script>
</body>
</html>        

In this example, clicking the "Copy Text" button will copy the text "Hello, Clipboard!" to the clipboard using the Clipboard API. We handle any errors that may occur during the copying process.

Browser Support:

It's important to note that browser support for the Clipboard API varies. As of writing, most modern browsers support this API, but it's always a good idea to check for compatibility before relying on it in production.


"The Clipboard API streamlines copy-paste functionality in web apps, enhancing user experience and interaction with content."

Happy Codding .... :) , Thank You

To view or add a comment, sign in

More articles by OMKESH B. KENDRE 👋

  • Unlocking Seamless Asynchronous Loading with React Suspense

    As web applications become more dynamic, the need for efficient data fetching and seamless user experiences has never…

  • Javascript Design Patterns

    JavaScript is a versatile and powerful programming language commonly used for web development. As applications become…

  • JavaScript Design Pattern

    History of Design Pattern Programming patterns have been around ever since they were created. But they were not…

  • Polyfills for forEach()

    What is Polyfill? Polyfill is a piece of code (or plugin) that provides the technology that you, the developer, expect…

  • Polyfills for filter()

    What is Polyfill? Polyfill is a piece of code (or plugin) that provides the technology that you, the developer, expect…

  • Polyfills for map()

    What is Polyfill? Polyfill is a piece of code (or plugin) that provides the technology that you, the developer, expect…

  • Storybook

    Storybook is an open-source UI expert tool to build UI components and pages. It is easy to test UI components during…

  • Web Content Accessibility

    What is Web Accessibility? The web and internet is a most important part of our life which includes government…

    1 Comment
  • NVM - Node version manager

    NVM (Node Version Manager by the user creationix on GitHub) is a tool that allows the user to switch between different…

Explore content categories