How to copy the text to clipboard in JavaScript7 Jan 2025 | 3 min read You may want to include the copy feature while creating complex web pages and applications. Instead of selecting the text and pressing a few keys on the keyboard to copy it, your users may now copy text by clicking a button or icon. When someone has to copy an activation code, recovery key, a snippet of code, etc., this capability is typically employed. To let the user know that the text has been copied to their clipboard, you may also include features like an alert or text on the screen (which could be a modal). In the past, the document.execCommand() command would have been used to accomplish this, but it has since been deprecated. The Clipboard API is now available, allowing you to asynchronously read from and write to the system clipboard as well as respond to clipboard activities (cut, copy, and paste). JavaScript's Clipboard API can be used to copy text to the clipboard. You can read from and write to the clipboard by using the Clipboard API, which offers a means to communicate with the clipboard. You will discover how to use the Clipboard API to write (copy) text and images to the clipboard in this tutorial. How to Examine the Permissions of the BrowserIt is crucial to be aware that only pages delivered over HTTPS are compatible with the Clipboard API. Before attempting to write to the clipboard, you should check your browser's permissions to make sure you have write access. The navigator will help you with this .permissions phrasing. Here's how you can copy text to the clipboard using the Clipboard API:Create a button or any element that will trigger the copy action. For example: Add an event listener to the button that will trigger the copy action. Inside the event listener, you will need to call the writeText method of the Clipboard API to write the text to the clipboard. For example: In the above example, we first select the button using document.querySelector, and then add an event listener to it using addEventListener. Inside the event listener, we call the writeText method of the Clipboard API, passing the text we want to copy as an argument. The writeText method returns a promise, so we use .then to handle the success case and .catch to handle any errors. Make sure to request the "clipboard-write" permission in your website. Without this permission, the navigator.clipboard.writeText will fail. Add the following line of code to your HTML file in the head section: That's it! When the user clicks the button, the text will be copied to the clipboard. Note that this method works on most modern browsers, but some older browsers may not support it. In that case, you may need to use a fallback method, such as using a hidden input field to store the text and then selecting it with document.execCommand('copy'). Example: In this case, the aforementioned strategy will be used. Output ![]() Here, click the button to copy the text from the text field. Try to paste the text (e.g. ctrl+v) afterwards in a different window, to see the effect. Next TopicJavascript documentfragment |
In JavaScript, there are two ways to copy objects: Shallow copy and Deep copy. Understanding the differences between these two types of copying can be important in many programming scenarios, as they each have their own strengths and weaknesses. Shallow Copy A shallow copy is a copy...
3 min read
What is Filter in JavaScript? In JavaScript, a filter is a method for arrays that allows you to create a new array containing only elements that pass a certain condition. It doesn't modify the original array instead it returns a new array with elements that satisfy the...
7 min read
In the realm of game development, picking the right engine can have a significant effect between a fruitful task and one that never arrives at its maximum capacity. Web creation has been totally changed by JavaScript, and its flexibility even stretches out to the universe of...
6 min read
What is Debugging in JavaScript? In JavaScript, debugging is used to find errors or bugs in the source code of any software. In simple words, debugging is a process in which we examine the JavaScript code find the errors, and fix them. Developers can make some mistakes...
4 min read
Sorting the arrays of objects in JavaScript is a basic manipulation and is also a really important part of programming which is used in day-to-day web development. JavaScript offers two built-in sorting methods: Array.prototype.sort and Array. Sort that both sort the elements in place, but the...
6 min read
What is Iteration in JavaScript? Iteration is a process that occurs when we execute the code for each item in the collections, basically for the element in the array or the properties of an object. With the help of the iterator, we can define the sequence and...
4 min read
The closest() method in JavaScript is used to retrieve the closest ancestor, or parent of the element matches the selectors. If there is no ancestor found, the method returns null. This method traverses the element and its parents in the document tree, and the traversing continues...
2 min read
Numerical value extraction from strings is a frequent activity in the field of JavaScript programming, particularly when working with user inputs or data processing. Being able to obtain these numerical values quickly is essential for a variety of tasks, including processing data supplied by users,...
4 min read
JavaScript provides a variety of operators to evaluate mathematical and logical operators and expressions in your programs. Increment and decrement operators are two kinds of unary operators in JavaScript that add or subtract a value of 1 from an operand. The purpose of this article is...
3 min read
JavaScript instanceof operator will find the object type at the time of runtime. The outcome we can expect can be Boolean values that are either true or false which are based on the inputs we gave and the objects that are included. Syntax: var instance = objectName...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India