Copy Text to Clipboard
JavaScript
Medium
2 views
Problem Description
You have . On click, copy data-copy text to clipboard and change button text to COPIED for 1 second.
Output Format
No output (DOM change).
Constraints
Assume buttons .copy exist. Modern browser clipboard API available.
Official Solution
document.querySelectorAll('button.copy').forEach(btn=>{btn.addEventListener('click',async()=>{const text=btn.dataset.copy||'';try{await navigator.clipboard.writeText(text);const old=btn.textContent;btn.textContent='COPIED';setTimeout(()=>{btn.textContent=old;},1000);}catch(e){}});});
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!