Copy highlighted code to clipboard in jekyll

I am creating a blog using jekyll. I am using prettyprint to highlight code snippets. I wrote jquery to display a button on hover of a code snippet (inside a <pre> tag). When the button is clicked, I get all the html of the code snippet, but I want to copy the clean text of the code snippet.

Can anyone advise me how to do this?

+3


source to share


1 answer


The function document.execCommand

can be used to copy text to the clipboard in JavaScript. jQuery is not required.



function copy() {
  var element = document.getElementById('input');

  element.select();
  document.execCommand('copy');
  element.blur();
}
      

<input id="input" />
<button onclick="copy()">Copy Text</button>
      

Run codeHide result


+1


source







All Articles