Created
April 28, 2019 12:48
-
-
Save MistrySaurabh/a78fee382c07e38371ed1e5ba84ace38 to your computer and use it in GitHub Desktop.
Copy To Clipboard Using Jquery and Javascript
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
// using JQuery | |
function copyToClipboard(element) { | |
var $temp = $("<input>"); | |
$("body").append($temp); | |
$temp.val($(element).text()).select(); | |
document.execCommand("copy"); | |
$temp.remove(); | |
} | |
// demo JQuery | |
<p id="p1">P1: I am paragraph 1</p> | |
<button onclick="copyToClipboard('#p1')">Copy P1</button> | |
// using Javascript | |
function copyToClipboard(elementId) { | |
var aux = document.createElement("input"); | |
aux.setAttribute("value", document.getElementById(elementId).innerHTML); | |
document.body.appendChild(aux); | |
aux.select(); | |
document.execCommand("copy"); | |
document.body.removeChild(aux); | |
} | |
// demo Javascript | |
<p id="p1">P1: I am paragraph 1</p> | |
<button onclick="copyToClipboard('p1')">Copy P1</button> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment