Copy to Clipboard with JavaScript – (also for hidden element)

In this trick, I’ll show you how you can copy to a clipboard with javascript.

We used the select() function for the selection of input value and then execCommand() function for “copy” command with javascript. It will print your copied text in the console terminal.

Note: The above function only work with visible input element. If you have hide the element with CSS property like visibility: hidden; OR display: none; then it will not work for that element.

So, for that element, you have to use another tricky way to make a copy to the clipboard function in javascript. Let’s see the code.

It will copy the data from the hidden element.

Being Tricky 😉

About the Author: Aman Mehra

Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+ years of experience. I love coding and help to people with this blog.


Leave a Reply

Your email address will not be published. Required fields are marked *