Last active
June 10, 2022 14:37
-
-
Save upadrian/13a5af1c2e016cffba4925a1703d197c to your computer and use it in GitHub Desktop.
Copy text to clipboard using javascript. jQuery version.
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 src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossOrigin="anonymous"></script> | |
| <script src="https://code.jquery.com/jquery-migrate-3.4.0.min.js" integrity="sha256-mBCu5+bVfYzOqpYyK4jm30ZxAZRomuErKEFJFIyrwvM=" crossOrigin="anonymous"></script> | |
| <p>Use our support | |
| <a href='mailto:support@domain.com'>support@domain.com</a> | |
| <a href='javascript:;' class='btnCopy'><input type='hidden' readonly value='support@domain.com'/><sup>⧉</sup></a> | |
| to contact us</p> | |
| <script> | |
| $(document).ready(function() { | |
| var $elements = $(".btnCopy"); | |
| $elements.each(function() { | |
| $(this).click(function(e) { | |
| e.preventDefault(); | |
| var $buttonElement = $(this), | |
| text = $("input", $buttonElement).val(); | |
| navigator | |
| .clipboard | |
| .writeText(text) | |
| .then(function() { | |
| alert("Copied to clipboard!"); | |
| }, function() { | |
| alert('Copying to clipboard failed!'); | |
| }); | |
| }); | |
| }); | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment