WeCode
Academy
Tutorials
Web Development
Bootstrap
CSS
HTML
JavaScript
jQuery
Node.js
PHP
React
TypeScript
Programming
C
C#
C++
Go
Java
Python
Rust
Data and Databases
NumPy
Pandas
SQL
Tools
Bash
Git
Projects
Project: A Tiny Shell in C
Project: Bank Account System
Project: Library Management System
Project: Portfolio Page
Project: Quiz App in React
Project: Sales Data Analysis
Project: Server Log Analyser
Project: To-Do REST API
Project: URL Shortener
Project: Word Counter in Four Languages
All tutorials ❯
Paths
References
Practice
Try it editor
Quizzes
How To recipes
Tools
Glossary
My Learning
🌙
Dark mode
Home
›
How To
›
JavaScript
Copy text to the clipboard
navigator.clipboard.writeText returns a promise; it needs a user action.
Example
html
<input id="t" value="Copy me"> <button id="b">Copy</button> <span id="m"></span> <script> document.getElementById("b").onclick = async function () { try { await navigator.clipboard.writeText(document.getElementById("t").value); document.getElementById("m").textContent = "Copied!"; } catch (e) { document.getElementById("m").textContent = "Clipboard not allowed here"; } }; </script>
Run ❯
Reset
Open in editor »
More JavaScript recipes
Debounce a function
Loop over an object
Remove duplicates from an array
Shuffle an array
Format a number as currency