Last active
November 14, 2016 09:19
-
-
Save maldechavda/d3c50c39b955329c4457741f097469b6 to your computer and use it in GitHub Desktop.
Copy TO Clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <input id="txt" /> | |
| <button id="btn">Copy To Clipboard</button> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script type="text/javascript"> | |
| $(document).ready(function() { | |
| function copyText(text){ | |
| function selectElementText(element) { | |
| if (document.selection) { | |
| var range = document.body.createTextRange(); | |
| range.moveToElementText(element); | |
| range.select(); | |
| } else if (window.getSelection) { | |
| var range = document.createRange(); | |
| range.selectNode(element); | |
| window.getSelection().removeAllRanges(); | |
| window.getSelection().addRange(range); | |
| } | |
| } | |
| var element = document.createElement('DIV'); | |
| element.textContent = text; | |
| document.body.appendChild(element); | |
| selectElementText(element); | |
| document.execCommand('copy'); | |
| element.remove(); | |
| } | |
| var txt = document.getElementById('txt'); | |
| var btn = document.getElementById('btn'); | |
| btn.addEventListener('click', function(){ | |
| copyText(txt.value); | |
| }) | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment