Skip to content

Instantly share code, notes, and snippets.

@roberthopman
Created January 11, 2022 16:26
Show Gist options
  • Save roberthopman/40d6fa0a6029d51a6745113b77674683 to your computer and use it in GitHub Desktop.
Save roberthopman/40d6fa0a6029d51a6745113b77674683 to your computer and use it in GitHub Desktop.
// optimized for rails
document.addEventListener("turbolinks:load", () => {
$('[data-toggle="tooltip"]').tooltip()
$('[data-toggle="popover"]').popover()
if (document.querySelector('#target-btn')) {
// start of the actual copy paste action
document.querySelector('#target-btn').addEventListener("click", (event) => {
var el = document.getElementById("target-table");
var body = document.body, range, sel;
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
sel.removeAllRanges();
try {
range.selectNodeContents(el);
sel.addRange(range);
} catch (e) {
range.selectNode(el);
sel.addRange(range);
}
} else if (body.createTextRange) {
range = body.createTextRange();
range.moveToElementText(el);
range.select();
}
var theButton = document.getElementById("target-btn");
try {
var ok = document.execCommand('copy');
if (ok) theButton.innerHTML = 'Copied!';
else theButton.innerHTML = 'Something went wrong.';
} catch (err) {
theButton.innerHTML = 'This browser is not supported.';
}
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment