1

This is what I have.

<p class="right-side-header" title="Copy Text" id="text">I am cool</p>

Now the question is, what JavaScript will allow me to copy whatever text is in the <p> tag. I am new to JavaScript. I have no idea how to go about this.

How can I copy the innerText of the above element to the clipboard?

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
SkylixMC
  • 132
  • 1
  • 2
  • 8

1 Answers1

22

You can use the same function from my answer here:

function copyElementText(id) {
    var text = document.getElementById(id).innerText;
    var elem = document.createElement("textarea");
    document.body.appendChild(elem);
    elem.value = text;
    elem.select();
    document.execCommand("copy");
    document.body.removeChild(elem);
}
<p class="right-side-header" title="Copy Text" id="text" onclick="copyElementText(this.id)">I am cool</p>
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79