Copy input text in clipboard using jQuery

You are currently viewing Copy input text in clipboard using jQuery

Copy input text in clipboard using jQuery

To copy text to the clipboard with jQuery, you can use the .select() and document.execCommand('copy') methods. Here is an example of how you might do this with a button:

Copy code$(document).ready(function(){
  $("#copy-button").click(function(){
    var copyText = $("#text-to-copy");
    copyText.select();
    document.execCommand("copy");
    alert("Copied the text: " + copyText.val());
  });
});

In this example, when the button with the ID “copy-button” is clicked, the text inside the element with the ID “text-to-copy” is selected and copied to the clipboard. An alert is then displayed to confirm that the text has been copied.

Please note that .select() and document.execCommand('copy') is not supported in all browsers.

You can also use clipboard.js or ZeroClipboard library to achieve this with more browser compatibility.

if you need any help in wordpress development click here

Leave a Reply