Skip to content

Instantly share code, notes, and snippets.

@MistrySaurabh
Created April 28, 2019 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MistrySaurabh/a78fee382c07e38371ed1e5ba84ace38 to your computer and use it in GitHub Desktop.
Save MistrySaurabh/a78fee382c07e38371ed1e5ba84ace38 to your computer and use it in GitHub Desktop.
Copy To Clipboard Using Jquery and Javascript
// 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