Javascript Copy To Clipboard Without Input Example


Share:
Published By: Admin, Published On: Apr 24, 2021, Category: Javascript

Hello Friends,

Now let's see example of how to copy to clipboard withou input in javascript. Here you will learn how to use javascript copy to clipboard withou input. We will withou input use how to copy to clipboard in javascript. This is a short guide on copy to clipboard withou input. Let's get started with how to copy to clipboard withou input in javascript.

Here i will give you many example how you can copy to clipboard withou input in javascript.

Example:
<!DOCTYPE html>
<html>
    <head>
        <title>Javascript - How to copt to clipboard without input?-MyWebtuts.com</title> 
        <meta charset="utf-8">
    </head>
    <body>

        <div id="secretInfo" style="display: none;">secret info</div>
        <button type="button" id="btnCopy">Copy hidden info</button>

        <script type="text/javascript">

            var $body = document.getElementsByTagName('body')[0];
            var $btnCopy = document.getElementById('btnCopy');
            var secretInfo = document.getElementById('secretInfo').innerHTML;

            var copyToClipboard = function(secretInfo) {
                var $tempInput = document.createElement('INPUT');
                $body.appendChild($tempInput);
                $tempInput.setAttribute('value', secretInfo)
                $tempInput.select();
                document.execCommand('copy');
                $body.removeChild($tempInput);
            }

            $btnCopy.addEventListener('click', function(ev) {
                copyToClipboard(secretInfo);
            });

    </script>

  </body>
</html>
Output:
Copy hiddden info

It will help you....

Share: