Copy function

A basic function to copy text from the page

HTML

<body>
<p id="CopyText">Text to be copied</p>
<button onclick="copyText();">Copy Text</button>
</body>

JavaScript

var copyText = function(){

	//selects the text from the document
	var selection = document.querySelector('#CopyText'); //Change the #CopyText for whatever you want to select
  var range = document.createRange(); //creates the range that we are going to add selection to
  range.selectNode(selection); //adds selection to the range
  window.getSelection().addRange(range); //selects the range
  
  //copy the text and the deselect it
  document.execCommand('copy'); //copy the selection
  
  window.getSelection().removeAllRanges(); //deselects the text
};