Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <style type="text/css">
- .copyInput {
- border: none;
- overflow: auto;
- outline: none;
- -webkit-box-shadow: none;
- -moz-box-shadow: none;
- box-shadow: none;
- width: 0px;
- height: 0px;
- position: absolute;
- opacity: 0;
- }
- #thaSpan {
- cursor: pointer;
- }
- </style>
- </head>
- <body>
- <span id="thaSpan">The brown lazy fox</span>
- <script>
- bindCopy('thaSpan');
- function bindCopy(spanSelector, callback) {
- let span = document.getElementById(spanSelector);
- span.addEventListener('click', copySpan, false);
- function copySpan() {
- let input = document.createElement('textarea');
- input.readOnly = true;
- input.className += ' copyInput';
- document.body.appendChild(input);
- input.value = span.innerHTML;
- input.select();
- document.execCommand("Copy");
- setTimeout(function() {
- input.selectionEnd = input.selectionStart;
- input.blur();
- input.parentNode.removeChild(input);
- if (typeof callback === 'function') callback();
- }, 1);
- }
- }
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment