Copy to clipboard example

Practical example on how to make copy to clipboard with java script

by sunil puvvada

HTML

<h1>Copy to clipboard example</h1>
<input type="text" id="copy-input" value="DotJord copy to clipboard!"/>
<input type="button" id="copy-button" value="Copy">

JavaScript

// Copy to clipboard example
document.querySelector("#copy-button").onclick = function() {
  // Select the content
  document.querySelector("#copy-input").select();
  // Copy to the clipboard
  document.execCommand('copy');
};