leham789 leham789 - 3 months ago 11
CSS Question

Click to copy text javascript

Let me start with that i'm a beginner on javascript and I just about understand a click toggle.
I am making a site like flatuicolors.com and it that when you click on a div, it will copy the colour. I have wrote each colour in the each div in a P tag and was wondering if you can click to copy the P tag in that div.

<div class="main-colour-container col-group-1">
<div class="colours-featured" id="div2">

<div class="colour-box" style="background:#F22613;">
<div class="inside-colour-box">
<div class="colour-box-text fade">
<p>#F22613</p>
</div>
</div>
</div>

<div class="colour-box" style="background:#F44336;">
<div class="inside-colour-box">
<div class="colour-box-text fade">
<p>#F44336</p>
</div>
</div>
</div>
<div class="colour-box" style="background:#C62828;">
<div class="inside-colour-box">
<div class="colour-box-text fade">
<p>#C62828</p>
</div>
</div>
</div>
<div class="colour-box" style="background:#DB3D45;">
<div class="inside-colour-box">
<div class="colour-box-text fade">
<p>#DB3D45</p>
</div>
</div>
</div>
</div>
</div>

Answer

Coincidentally I ahd made the exact same PEN once.

Here is the link. Hope it helps. :)

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