--- url: "https://web.dev/articles/clipboard/copy-text" titulek: "How to copy text | Clipboard | web.dev" h1: "How to copy text Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/clipboard/copy-text" drobecky: ["Clipboard", "How to copy text"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 01:49:11" znaku: 4107 slov: 479 kodovani: "iso-8859-2" sha256: "7be94e844b1e3cddc8d884596861aa9b0c8b1a0e96bc87c69e5d8b71a3960446" --- How to copy text Stay organized with collections Save and categorize content based on your preferences. Joe Medley Cutting and pasting text is one of the most commonly used features of apps in general and desktop applications in particular. How do I copy text on the web? There's an old way and a new way. And it depends on which browser is used. The modern way Using the Async Clipboard API The [`Clipboard.writeText()`](/articles/clipboard/copy-text/%E2%80%8B%E2%80%8Bhttps:/developer.mozilla.org/docs/Web/API/Clipboard/writeText) method takes a string and returns a Promise that resolves when text is successfully written to the clipboard. `Clipboard.writeText()` can only be used from the `window` object that has focus. Browser Support 66 79 63 13.1 [Source](https://developer.mozilla.org/docs/Web/API/Clipboard/writeText) The classic way Using `document.execCommand()` Calling [`document.execCommand('copy')`](https://developer.mozilla.org/docs/Web/API/Document/execCommand#copy) returns a boolean value that indicates whether the copy was successful. Call this command inside of a user gesture such as a [click handler](https://developer.mozilla.org/docs/Web/API/Element/click_event#javascript). This approach has limitations when compared to the Async Clipboard API. The `execCommand()` method only works with DOM elements. Because it's synchronous, copying large amounts of data, especially data that must be transformed or sanitized in some way, can block the page. Browser Support 1 12 69 1.3 [Source](https://developer.mozilla.org/docs/Web/API/Document/execCommand) Progressive enhancement `const copyButton = document.querySelector('#copyButton'); const out = document.querySelector('#out'); if ('clipboard' in navigator) { copyButton.addEventListener('click', () => { navigator.clipboard.writeText(out.value) .then(() => { console.log('Text copied'); }) .catch((err) => console.error(err.name, err.message)); }); } else { copyButton.addEventListener('click', () => { const textArea = document.createElement('textarea'); textArea.value = out.value; textArea.style.opacity = 0; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { const success = document.execCommand('copy'); console.log(`Text copy was ${success ? 'successful' : 'unsuccessful'}.`); } catch (err) { console.error(err.name, err.message); } document.body.removeChild(textArea); }); }` Further reading [Unblocking clipboard access](/async-clipboard) (the modern way) [Cut and copy commands](https://developer.chrome.com/blog/cut-and-copy-commands) (the classic way) Demo HTML ` How to copy text

How to copy text

` CSS `:root { color-scheme: dark light; } html { box-sizing: border-box; } *, *:before, *:after { box-sizing: inherit; } body { margin: 1rem; font-family: system-ui, sans-serif; } button { display: block; }` JS `const textarea = document.querySelector('textarea'); const button = document.querySelector('button'); button.addEventListener('click', async () => { try { await navigator.clipboard.writeText(textarea.value); } catch (err) { console.error(err.name, err.message); } });` Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2026-09-04 UTC.