Elepsis Eclipse
Гений
(64087)
4 года назад
setAutoCopy();
function setAutoCopy() {
let inp = document.createElement('input');
display(inp, 'none');
document.body.appendChild(inp);
let copy = document.querySelectorAll('.copy');
for (let i = 0; i < copy.length; i++) {
copy[i].addEventListener('click', function() {
autoCopy(this, this.textContent);
});
}
function autoCopy(btn, text) {
display(inp);
inp.value = text;
inp.select();
document.execCommand('copy');
display(inp, 'none');
btn.classList.add('copied');
setTimeout(function() {
btn.classList.remove('copied');
}, 2000);
}
}
function display(elems, value) {
value = value || "block";
if (elems instanceof HTMLElement) {
elems.style.display = value;
return;
}
for (let i = 0; i < elems.length; i++) {
elems[i].style.display = value;
}
}
Источник: Кусок кода, что был под рукой.