All TalkersCode Topics

Follow TalkersCode On Social Media

JavaScript Copy To Clipboard

Last Updated : Jul 1, 2023

JavaScript Copy To Clipboard

In this tutorial we will show you the solution of JavaScript copy to clipboard, here we needs to copy to clip board with input tag by using navigator object of window and select(), writeText() methods.

For select that text we have to use ‘select()’ method and ‘setSelectionRange’ method will help us to copy text from DOM element particular range and we appending that value on alert box.

Step By Step Guide On JavaScript Copy To Clipboard :-

Here we used select(), setSelectionRange() and writeText() these methods for achieve result.

The select() method selects all the text in a <textarea> element or in an <input> element that includes a text field so we using that to selecting input field text.

As we seen setSelectionRange() method will covers text from input field of elements and writeText() used to write on clip board copied text then we appended that on alert box.

<!DOCTYPE html>
        <title>COPY TO CLIPBOARD</title>
        <input type="text" id="inp" value="Hello">
        <button onclick="c2cb()">Copy Text</button>
            function c2cb(){
                var ct=document.getElementById("inp");
                alert("Copied Text: "+ct.value);
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined input tag with attributes ‘id, value’ with respective values ‘inp, Hello’ and button tag ‘Copy Text’ with onclick event there we specifying function ‘c2cb’.
  7. In script we defined function ‘c2cb()’, here using id ‘inp’ we collecting input value ‘Hello’ and stored on variable ‘ct’.
  8. The select() we seen above help us to select input string ‘Hello’ and setSelectionRange() will used specifies start point ‘0’ to end ‘99999’ to cover input field ‘ct’ defined in webpage.
  9. The navigator object used to access browsers information so we used that with clipboard API to interface the read-only clipboard property which returns clipboard object used to read or write the clipboard contents.
  10. Now we writing ‘ct’ value on clipboard by ‘writeText()’ method and we appended result message on alert box.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to copy to clipboard using javascript.

When we executes program on browser we can see input box with text ‘Hello’ and button ‘Copy Text’ when user clicks this button alert box will pop up with string ‘Copied Text: Hello’ of result.

We can modify specified string to your choice result won’t get affect we gets successful process of copied to clipboard.

We can also do copy to clipboard without input box later we will discuss about them.

I hope this tutorial on JavaScript copy to clipboard helps you and the steps and method mentioned above are easy to follow and implement.