I am currently struggling with copying to clipboard via jQuery.
The code I have works if I use via on-click="Myfunction"
, but since I need to use it in several places, I would like to apply the function to all elements with a class. This is what I have tried.
$(".btn-clipboard").click(function(e){
copyToClipboard($(this));
});
function copyToClipboard(element) {
var $temp = $("<textarea>");
$("body").append($temp);
$temp.val($(element).siblings("code").text()).select();
document.execCommand("copy");
$temp.remove();
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bd-clipboard">
<button class="btn-clipboard" title="" data-original-title="Copy to clipboard" >Copy</button>
</div>
<figure>
<pre>
<code class="language-html" data-lang="html"><br>
<span> code example that should be copied</span>
</code>
</pre>
</figure>