How to Copy and Paste Text in Javascript
In this tutorial, you will learn how to copy and paste text in javascript. Whenever we want to copy a text, we make use of the keyboard shortcut CTR+C
and to paste the text, we make use of CTR+V
. The same thing can be achieved using a mouse, but most of the users prefer doing so with the help of a keyboard.
If you are a newbie, then copying and pasting text content from one input field to another input field using javascript is a bit tricky. There are third-party javascript libraries such as Clipboard JS which could help in copying the text, but I could not find any library that could help in pasting the text.
To accomplish our goal, we are going to make use of Clipboard API. This API is used to asynchronously read from and write to the system clipboard. It is accessible via the clipboard property of the Navigator interface.
In the following example, we have 2 textarea and 2 button elements. Upon click of the copy button, we will copy the text from the first textarea element and upon click of a paste button, we will paste the copied text into the second textarea element. Please have a look over the code example and the steps given below.
HTML & CSS
- We have 3 elements in the HTML file (
div
,textarea
, andbutton
). Thediv
element with a class ofcontainer
is just a wrapper for the rest of the elements. - We have 2
button
elements with aninnerText
of“Copy”
and“Paste”
. - We have done some basic styling using CSS and added the link to our
style.css
stylesheet inside thehead
element. - We have also included our javascript file
script.js
with ascript
tag at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="style.css"> <title>Document</title> </head> <body> <div class="container"> <div> <textarea id="copyArea" cols="30" rows="10"></textarea><br> <button id="copy">Copy</button> </div> <div> <textarea id="pasteArea" cols="30" rows="10"></textarea><br> <button id="paste">Paste</button> </div> </div> <script src="script.js"></script> </body> </html>
.container { display: flex; }
Javascript
- We have selected the
button
element with an id ofcopy
usinggetElementById()
and attached aclick
event listener to it. - There are 2 solutions for copying the text from the
textarea
element. - In first solution, we are selecting entire text in the first
textarea
element by usinggetElementById()
andselect()
methods. Then, we are executing the copy command by callingexecCommand()
method and passing the string“copy”
as a parameter. - In the second solution, we are selecting the first
textarea
element by using thegetElementById()
method and storing it in thecopyArea
variable. We are copying text to the clipboard by callingnavigator.clipboard.writeText()
method and passing the value of thetextarea
element as a parameter. - We have selected the
button
element with an id ofpaste
usinggetElementById()
and attached aclick
event listener to it. - We are selecting the second
textarea
element by using thegetElementById()
method and storing it in thepasteArea
variable. - We are setting empty string as its value to make sure the
textarea
element is empty. - We are using the asynchronous
navigator.clipboard.readText()
method to read the text from the clipboard. After reading the text, we are setting it as a value of the secondtextarea
element.
document.getElementById('copy').addEventListener('click', ()=>{ /* document.getElementById('copyArea').select(); document.execCommand('copy'); */ let copyArea = document.getElementById('copyArea'); navigator.clipboard.writeText(copyArea.value); }); document.getElementById('paste').addEventListener('click', ()=>{ let pasteArea = document.getElementById('pasteArea'); pasteArea.value = ''; navigator.clipboard.readText() .then((text)=>{ pasteArea.value = text; }); });