All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

JavaScript Copy To Clipboard Without Input

Last Updated : Mar 11, 2024

JavaScript Copy To Clipboard Without Input

In this tutorial we will show you the solution of JavaScript copy to clipboard without input, here we need to copy to clip board without input tag so we are creating input element at script blog then we appending them on body of webpage.

For select that text we have to use ‘select()’ method and ‘execCommand("copy")’ method will help us to copy text from webpage and we appending that value on alert box.

Step By Step Guide On JavaScript Copy To Clipboard Without Input :-

Here we used select() and execCommand("copy") 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.

The execCommand("copy") method used to replace the clipboard’s content with selected material and these commands can be used without any special permission in short-lived event handlers for a user action so we used this for copy our specified text then appended on alert box.

<!DOCTYPE html>
        <title>COPY TO CLIPBOARD WITHOUT INPUT</title>
        <button onclick="c2cb('Hi Everyone')">Copy Text</button>
            function c2cb(ele){
                var tempTxt=document.createElement("input");
                alert("Copied Text: "+tempTxt.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 a button tag ‘Copy Text’ with onclick event there we specifying function ‘c2cb’ with some text ‘Hi Everyone’ for append to clipboard.
  7. In script we defined function ‘c2cb(ele)’, here ‘ele’ refers text ‘Hi Everyone’. We defined variable ‘tempTxt’ for refers created ‘input’ element.
  8. Using ‘value’ property we sets ‘ele’ to ‘tempTxt’ value then we appending that input field with value ‘Hi Everyone’ on body of webpage by appendChild().
  9. The select() we seen above help us to select input string ‘Hi Everyone’ and execCommand() will copies DOM element values then removing tempTxt from webpage for append to clipboard.
  10. Finally we appended ‘Hi Everyone’ on alert box with string ‘Copied Text:’.
  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 without input tag using javascript.

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

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

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

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪