DEV Community

Cover image for Copy text to the clipboard with JavaScript in 10 lines of code

Copy text to the clipboard with JavaScript in 10 lines of code

AlbertoM on March 01, 2020

This article was originally posted on my blog. Head over to inspiredwebdev.com for more articles and tutorials. Check out my JavaScript course on ...
Collapse
 
jonrandy profile image
Jon Randy 🎖️

Might be worthwhile discussing navigator.clipboard too

Collapse
 
albertomontalesi profile image
AlbertoM

You need to add it to the DOM so that you can then select its content. As I said, if the element you want to copy to clipboard is an input field you don't need to do that.

Collapse
 
_akshaym profile image
Akshay Mahajan

I built a reusable React component using the same method. You can check it out at npmjs.com/package/react-copy-content

Collapse
 
trippymonk profile image
Blake Stansell

This is cool Is it how various web clippers work? Like Evernote and Notion?