document.addEventListener("click", function (event) { var button = event.target.closest("[data-copy]"); if (!button) return; var field = document.getElementById(button.dataset.copy); if (!field) return; function done() { var original = button.textContent; button.textContent = "Copied"; setTimeout(function () { button.textContent = original; }, 1500); } field.select(); field.setSelectionRange(0, field.value.length); // iOS needs the explicit range if (navigator.clipboard) { navigator.clipboard.writeText(field.value).then(done, function () { if (document.execCommand("copy")) done(); }); } else if (document.execCommand("copy")) { // Older browsers, and any context the async clipboard API is unavailable in. done(); } });