버튼 클릭 시 Table의 td 값을 Clipboard에 복사 하기

[ CSS ]
.hidden {
   position: fixed;
   bottom: 0;
   right: 0;
   pointer-events: none;
   opacity: 0;
   transform: scale(0);
}


[ HTML ]
<tr>
  <td class="">UserName</td>
  <td id="username" class="username">1234567</td>
  <td>
        <button style="display:inline" id="copy_username" type="button" class="btn btn-warning">copy</button>
        <input class="clipboard hidden" />
    </td>
</tr>

[ Javascipt ]
$("#copy_username").click(function() {
   var aux = document.createElement("input");
   copyText = document.getElementById("username").innerHTML;

   // Get the text from the element passed into the input
   aux.setAttribute("value", copyText);

   // Append the aux input to the body
   document.body.appendChild(aux);

   // Highlight the content
   aux.select();

   // Execute the copy command
   document.execCommand("copy");

   // Remove the input from the body
   document.body.removeChild(aux);

   alert('Copy Username to Clipboard : ' + copyText );
});


+ Recent posts