FormsLesson 131Free preview
Copy Text to Clipboard
This lesson is part of How To · All levels
Summary
Copy text to clipboard is a feature for copying text to clipboard with one click. The modern navigator.clipboard.writeText API returns Promise so that its success or failure can be handled. This feature is only available in secure contexts such as HTTPS or localhost.
Key points
- Use navigator.clipboard.writeText(text) to copy the text.
- The method returns Promise, so it can be used with async/await.
- The clipboard API requires a secure context (HTTPS or localhost).
- Handle permissions or browser errors that don't support try/catch.
- Give visual feedback once the text is copied.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.