[].slice.call(document.querySelectorAll('.block-copy')).map(function(el) { let button = el.querySelector('button'); button.addEventListener('click', () => { navigator.clipboard.writeText(el.querySelector('.block-copy-text').textContent) .then(() => { button.textContent = button.getAttribute('data-copy-success'); setTimeout(function () { button.textContent = button.getAttribute('data-default'); }, 3000); }) .catch(err => { button.textContent = button.getAttribute('data-copy-error'); setTimeout(function () { button.textContent = button.getAttribute('data-default'); }, 3000); console.log('Something went wrong', err); }); }); });