1document.getElementById("cp_btn").addEventListener("click", copy_password);
2
3function copy_password() {
4 var copyText = document.getElementById("pwd_spn");
5 var textArea = document.createElement("textarea");
6 textArea.value = copyText.textContent;
7 document.body.appendChild(textArea);
8 textArea.select();
9 document.execCommand("Copy");
10 textArea.remove();
11}
1// Copy to clipboard on a click event
2document.querySelector("#copy-button").addEventListener('click', function() {
3 var reference_element = document.querySelector('#to-select-text');
4
5 var range = document.createRange();
6 range.selectNodeContents(reference_element);
7
8 window.getSelection().addRange(range);
9
10 var success = document.execCommand('copy');
11 if(success)
12 console.log('Successfully copied to clipboard');
13 else
14 console.log('Unable to copy to clipboard');
15
16 window.getSelection().removeRange(range);
17});
18
1<span id="to-select-text">1111 - 2222 - 3333 - 4444</span>
2<button id="copy-button">Copy</button>