Advertisement
lemansky

Untitled

Mar 21st, 2024
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.96 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Copy Code to Clipboard</title>
  7.     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
  8. </head>
  9. <body>
  10.     <div class="container">
  11.         <div class="row justify-content-center">
  12.           <div class="card w-50 bg-secondary-subtle p-3">
  13.             <div class="d-flex justify-content-between">
  14.                 <pre id="code">const message = "Hello, World!";</pre>
  15.                     <a class="copy-button text-end" href='#'>
  16.                         <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-copy" viewBox="0 0 16 16">
  17.                             <path fill-rule="evenodd" d="M4 2a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2zm2-1a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1zM2 5a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1v-1h1v1a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h1v1z"/>
  18.                         </svg>
  19.                     </a>
  20.             </div>
  21.            
  22.             </div>
  23.         </div>
  24.        
  25.     </div>
  26.     <script>
  27.         document.querySelector('.copy-button').addEventListener('click', (e) => {
  28.             let codeElement = document.getElementById('code');
  29.             let code = codeElement.innerText;
  30.             navigator.clipboard.writeText(code);
  31.  
  32.            
  33.             let el = document.createElement('span');
  34.             el.innerText = 'Copied!';
  35.             el.classList.add('alert', 'alert-success', 'p-1', 'small', 'position-absolute', 'start-50', 'bottom-0');
  36.             codeElement.parentNode.append(el);
  37.             setTimeout(() => { el.remove(); }, 1000);
  38.         });
  39.                
  40.     </script>
  41. </body>
  42. </html>
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement