Just The Code Please

How to copy text to the clipboard using Javascript

January 16th 2024

Summary

Copying text to the clipboard is something users to all the time. To make life a little easier on them, it can be good to provide them with a button which copies specific text to the clipboard. Here is how to implement this kind of button in Javascript.

The Code

HTML
<button id="btn-id">Copy</button>
Javascript
const button = document.getElementById("btn-id");
button.addEventListener('click',function () {
    navigator.clipboard.writeText("Special text to copy");
});