Advertisement
bramanto

Copy Button

Oct 28th, 2021
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <title>Hello World</title>
  5. </head>
  6. <body>
  7.     <span id="datamandiri" onclick="pakaimandiri()">1234567890</span>
  8.     <br>
  9.     <br>
  10.     <button type="button" id="copy" onclick="pakaimandiri()">
  11.         Copy Data
  12.     </button>
  13.     <script>
  14.         var mandiri = document.getElementById('datamandiri');
  15.        
  16.         function pakaimandiri(){
  17.             /* TEKS YANG MAU DI COPY, DI SELECT DULU */
  18.             pilihteks('datamandiri');
  19.            
  20.             /* PROSES COPY */
  21.             document.execCommand('copy');
  22.            
  23.             /* UNTUK ANTISIPASI BEBERAPA TIPE BROWSER YANG GA SUPPORT execCommand */
  24.             //navigator.clipboard.writeText(mandiri.textContent);
  25.            
  26.             /* UNTUK MENGGANTI TULISAN DI TOMBOL */
  27.             document.getElementById('copy').innerHTML = 'Copied';
  28.            
  29.             alert('Norek Mandiri: ' + mandiri.textContent + ' berhasil disalin');
  30.         }
  31.        
  32.         /* UNTUK MENYELEKSI TEKS YANG AKAN DICOPY*/
  33.         function pilihteks(containerid) {
  34.             if (document.selection) { // INTERNET EXPLORER/EDGE
  35.                 var range = document.body.createTextRange();
  36.                 range.moveToElementText(document.getElementById(containerid));
  37.                 range.select();
  38.             } else if (window.getSelection) {
  39.                 var range = document.createRange();
  40.                 range.selectNode(document.getElementById(containerid));
  41.                 window.getSelection().removeAllRanges();
  42.                 window.getSelection().addRange(range);
  43.             }
  44.         }
  45.     </script>
  46. </body>
  47. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement