JSFiddle - React, Tailwind, and code Playground
HTML
<p id="p1">P1: I am paragraph 1</p>
<p id="p2">P2: I am a second paragraph</p>
<button onclick="copyToClipboard('#p1')">Copy P1</button>
<button onclick="copyToClipboard('#p2')">Copy P2</button>
<br/>
<br/>
<input type="text" placeholder="Paste here for test" />
JavaScript
copyToClipboard = function (element) {
var $temp = $("<input />");
$("body").append($temp);
$temp.val($(element).text()).select();
var result = false;
try {
result = document.execCommand("copy");
} catch (err) {
console.log("Copy error: " + err);
}
$temp.remove();
return result;
}