

Copy text to clipboard in JS(two ways)
source link: https://dev.to/0shuvo0/copy-text-to-clipboard-in-jstwo-ways-1pn1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Of course the user can just select the text and copy it but giving them a button that they can click to copy it seems like a better idea(better UX). So in this article I will show you how you can copy text to clipboard using JavaScript.
1. Old method
the old way to copy text to clipboard is
- create a textarea(Or input)
- put the text you want to copy in that textarea
- add textarea to your page
- select the text in textarea
- execute copy command
- remove it from your page
function copyToClipboard(text){
const textArea = document.createElement("textarea")
textArea.value = text
document.body.appendChild(textArea)
textArea.focus()
textArea.select()
document.execCommand('copy')
document.body.removeChild(textArea)
}
2. New method
For newer browsers we can simply use the navigator.clipboard
to copy text
function copyToClipboard(text){
navigator.clipboard.writeText(text)
}
3. Bonus
Now we can combine these two approach and create a function that would use the navigator.clipboard
in modern browsers and fall back to old approach when needed
function copyToClipboard(text){
if(navigator.clipboard){
navigator.clipboard.writeText(text)
return //codes below wont be executed
}
const textArea = document.createElement("textarea")
textArea.value = text
document.body.appendChild(textArea)
textArea.focus()
textArea.select()
document.execCommand('copy')
document.body.removeChild(textArea)
}
Make sure you check out my other articles and YouTube channel.
Recommend
-
74
How to Add a Copy-to-Clipboard Button to Your Jekyll Blog I’m always looking for ways to improve my site’s user experience without toppling the precarious house of cards that is cross-browser compatibility. And one...
-
13
Quick Tip - Copy The Output Of The Last PowerShell Command To Clipboard I recently found myself poking around in PowerShell and going “oh, good now I want to copy and paste that output into an email/dialog box/tweet/note...
-
10
How to Add Copy to Clipboard Buttons to Code Blocks in Hugo March 22, 2019 · 989 words · ~5 minutes to read A small quality of life improvement for programming-related websites...
-
150
How to Add a Copy-to-Clipboard Button to Jekyll I’m always looking for ways to improve my site’s user experience without toppling the precarious house of cards that is cross-browser compatibility. And one thing that...
-
22
Copy from Remote Server to Local Clipboard via OSC 52 in Neovim2021-01-05Nvim264 words 2 mins read 10 times readIn my daily work, I usually log into a...
-
11
Build a Blazor 'Copy to Clipboard' component with a Markdown editor Let's build a 'Copy to Clipboard' component, where we can copy Mar...
-
14
Mathys van der Merwe November 21, 2021 1 minute read ...
-
13
Nick Frostbutter Posted on Nov 28 ...
-
7
[JavaScript] Copy to Clipboard April 09, 2016 Copy content of
-
6
How to Copy Text and Images to Clipboard in Javascript? Jun 9, 2022 , by Agnidipta Bhattacharjee and
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK