Copy to clipboard pure JavaScript

Copy text from <p> element into clipboard using only JavaScript

by akmiecik

HTML

<body class="styler-loaded vsc-initialized" data-new-gr-c-s-check-loaded="14.1068.0" data-gr-ext-installed=""><div style="clear:both"></div><div id="content-body-wrap" class="container_16" style="min-height: 683px;">
<div id="content-body">



<p>Thursday, July 14</p>
<div class="clear"></div>
<h1 style="color:#900"><span style="font-weight:normal">Spiritual Warfare</span><br>
<span style="font-size:18px">Help Me Withstand Temptations</span></h1>
<p>[St.&nbsp;Faustina was tempted repeatedly by Satan against her vocation of proclaiming God's mercy and praying for mercy for souls, especially sinners (see <em>Diary</em>,&nbsp;1496-1499):]<br>
<br>
At that moment, I saw Jesus, who said, <strong>I am pleased with what you are doing. And you can continue to be at peace if you always do the best you can in respect to this work of mercy. Be absolutely as frank as possible with your confessor.<br>
<br>
Satan gained nothing by tempting you, because you did not enter into conversation with him. Continue to act in this way. You gave Me great glory today by fighting so faithfully</strong> (<em>Diary</em>,&nbsp;1499).
</p>



<p><em>Prayer:</em><br>
Lord, may I be faithful to my vocation at times of temptation. May I not enter into temptation by conversation with the Evil One. By the grace of Your presence, help me to withstand temptations.	</p>

<div class="clear" style="height:10px;"></div>

<div class="clear" style="height:10px;"></div>
</div>
</div><div style="clear:both"></div><script>(function(){var js = "window['__CF$cv$params']={r:'72a8c3e52bab636c',m:'vJppz741zdDNie5dfV0o5hfZARs_eTE4_othtxeTgHo-1657786051-0-AYib2dwGisLEQmQ46TS8rdyOMpegKtPdxaYUqhChRD/ifLrgHPBSkbdt0T2R9WDFdQLyyEuecmyImmLDEVrZpcxoWQcAliuVaZOXEco0SlU1P/UsSi6Fpe83o+gy2TM/fQ==',s:[0x899d142da2,0x97813705c7],u:'/cdn-cgi/challenge-platform/h/g'};var...

JavaScript

function copyToClipboard(elementId) {

  // Create an auxiliary hidden input
  var aux = document.createElement("input");

  // Get the text from the element passed into the input
  aux.setAttribute("value", document.getElementById(content-body).innerHTML);

  // Append the aux input to the body
  document.body.appendChild(aux);

  // Highlight the content
  aux.select();

  // Execute the copy command
  document.execCommand("copy");

  // Remove the input from the body
  document.body.removeChild(aux);

}

function log(){
	console.log('---')
}