jQuery Snippets: Copy to clipboard (onClick)

jQuery:

function copyToClipboard(element) {
	var $temp = $("<input>");
	$("body").append($temp);
	$temp.val($(element).text()).select();
	document.execCommand("copy");
	$temp.remove();
}

HTML Example:

<p id="p1">P1 text to copy</p>
<button onclick="copyToClipboard('#p1')">Copy P1</button>

<!-- shorter variation -->
<p id="p1" 
onclick="copyToClipboard('#p1')" 
style="cursor: copy">
P1 text to copy
</p>