0
    <button onclick="copyToClipboard('Rakib')">Copy TEXT 1</button>


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

I want to pass the value from onclick function and to copy that

Amman
  • 1
  • please refer [https://stackoverflow.com/questions/33855641/copy-output-of-a-javascript-variable-to-the-clipboard](https://stackoverflow.com/questions/33855641/copy-output-of-a-javascript-variable-to-the-clipboard). – Dhruvi Makvana Oct 31 '20 at 06:53

1 Answers1

0

You can create a hidden input where you can fill your text and copy it from there.

The only challenge is you can not fill values to hidden input and then select + copy. You first need to select, copy from it, and then set the input type to hidden.

function copyText(text) {
 var placeHolder = document.createElement("input");
 documeny.body.appendChild(placeHolder);
 placeHolder.setAttribute("id", "placeHolder");
 document.getElementById("placeHolder").value=text;
 placeHolder.select();
 document.execCommand("copy");
 document.getElementById("placeHolder").type ='hidden';
}
<button type="button" onclick="copyText('copied text')">Copy</button>
Dhruvi Makvana
  • 895
  • 5
  • 17