DEV Community

Cover image for COPY to the clipboard in JS
Mohsen Fallahnejad
Mohsen Fallahnejad

Posted on

COPY to the clipboard in JS

To set a textContent to the clipboard, you should get the desired element from DOM (eg. document.getElementById(“element-id”)) and access to its content:
alt text
then you should set your text to the clipboard :
alt text
and when this function is executed, it’s done…!

If you found it useful, you can follow me and also visit my website, because there are more ways to connect with me. I hope this would help you to improve your coding skills. Thank you for your attention.


This article was originally published on The Dev.to on April 8, 2020 and was written by Mohsen Fallahnejad.

Top comments (3)

Collapse
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

It's probably still a good idea to use the temporary textarea hack described here:

stackoverflow.com/a/46822033/5323344

window.navigator.clipboard is not yet supported in Microsoft Edge. It is supported in Firefox and Chrome, though.

Collapse
 
1e4_ profile image
Ian

When you thought dropping IE was enough. Edge still falls short in a bunch of aspects for dev :(

Collapse
 
mohsenfallahnjd profile image
Mohsen Fallahnejad

that's true
I have seen this way
And I just wrote the solution that I worked with
But in any case, what you said is true
Thank you for your attention and reminding