Copying data from your site to the user's clipboard is a small feature but can be very useful. Indeed, thanks to it no need to manually select the text or to use CTRL + C. This saves time for site visitors.

I will explain in this article how I realized this functionality in javascript to allow users to copy the CSS code of the elements of the site.

Method: Clipboard API.

The method I chose uses the browser clipboard API. This is a fairly new method but is supported by all the latest browsers. The advantage is that it is quite simple and only requires one line of javascript code.

navigator.clipboard.writeText("String to copy")

That's all ! Hope this can help you the next time you need to copy some text to the clipboard in javascript.