Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Copy Code to Clipboard</title>
- <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
- </head>
- <body>
- <div class="container">
- <div class="row justify-content-center">
- <div class="card w-50 bg-secondary-subtle p-3">
- <div class="d-flex justify-content-between">
- <pre id="code">const message = "Hello, World!";</pre>
- <a class="copy-button text-end" href='#'>
- <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-copy" viewBox="0 0 16 16">
- <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"/>
- </svg>
- </a>
- </div>
- </div>
- </div>
- </div>
- <script>
- document.querySelector('.copy-button').addEventListener('click', (e) => {
- let codeElement = document.getElementById('code');
- let code = codeElement.innerText;
- navigator.clipboard.writeText(code);
- let el = document.createElement('span');
- el.innerText = 'Copied!';
- el.classList.add('alert', 'alert-success', 'p-1', 'small', 'position-absolute', 'start-50', 'bottom-0');
- codeElement.parentNode.append(el);
- setTimeout(() => { el.remove(); }, 1000);
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement