Goal
To create a copy button for my Math.SE comment template in order to save the trouble of copying and pasting.
My first attempt
I put the boilerplate inside a Markdown codeblock to prevent them from getting interpreted by Hugo’s Markdown parser. Under each codeblock, I placed the copy button.
Comment boilerplate goes here ...
Another comment boilerplate goes here ...
…
My page’s original layout
$(document).ready(function() {
$('.copyBtn').click(function() {
copy($(this).prev().children())
j });
});
function copy(selector) {
var screenTop = $(document).scrollTop();
var $temp = $("<div>");
$("body").append($temp);
$temp.attr("contenteditable", true)
.html($(selector).html()).select()
.on("focus", function() { document.execCommand('selectAll',false,null) })
.focus();
document.execCommand("copy");
$temp.remove();
$('html, body').scrollTop(screenTop);
}
static/js/copyBtn.js
at Git tag copyBtn0