<div class="input-wrapper">
  <button onclick="saveToClipboard('Hello world!')">Copy to clipboard!</button>
</div>
.input-wrapper {
  text-align: center;
}
saveToClipboard = str => {
  const element = document.createElement('textarea');
  element.value = str;
  document.body.appendChild(element);
  element.select();
  document.execCommand('copy');
  document.body.removeChild(element);
  
  alert(`${str}' is copied to the clipboard!`);
};
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.