Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying to clipboard textbox value using jQuery/JavaScript

I have a textbox & button which looks like this:

     <div class="col-xs-11" style="padding:20px 0 ">
     <input type="text" class="form-control txtKeywords" id="txtKeyw" style="margin-bottom:10px; height:45px;" maxlength="80" placeholder="Click on keywords to combine your title">
   <button type="submit" class="btn btn-space btn-success btn-shade4 btn-lg copyToClipboard">
    <i class="icon icon-left s7-mouse"></i> Copy to Clipboard
     /button>

And when the user clicks the button copy to clipboard, I'd like to copy the contents of the textbox into the clipboard like this:

$(document).on("click", ".copyToClipboard", function () {
    copyToClipboard("txtKeyw");
    successMessage();
});

Where the definition of the copyToClipboard function is:

 function copyToClipboard(element) {
            var $temp = $("<input>");
            $("body").append($temp);
            $temp.val($(element).text()).select();
            document.execCommand("copy");
            $temp.remove();
        }

But when I do this, nothing happens -- no values are copied to clipboard from the textbox... What am I doing wrong here?

MORE INFORMATION:

  • This happens in both Chrome 59 64-bit and Firefox 54 32-bit.
  • successMessage() is called and displayed in the browser.
  • Adding # in front of the element's ID does not resolve the issue.
like image 202
User987 Avatar asked Jul 03 '17 15:07

User987


People also ask

How do I copy to the clipboard in jquery?

All you need to do is add data-clipboard-target="#toCopyElement" on any button, initialize it new Clipboard('. btn'); and it will copy the content of DOM with id toCopyElement to clipboard.

How can copy textbox value from another in javascript using checkbox?

You can write a javascript function to copy value from one textbox into the other textbox, and call the same function on “onclick” event of the checkbox. Same function can be used to clear the textbox on unchecking the checkbox.


2 Answers

STEP 1: Change your copyToClipboard(element) like this:

function copyToClipboard(text) {

   var textArea = document.createElement( "textarea" );
   textArea.value = text;
   document.body.appendChild( textArea );       
   textArea.select();

   try {
      var successful = document.execCommand( 'copy' );
      var msg = successful ? 'successful' : 'unsuccessful';
      console.log('Copying text command was ' + msg);
   } catch (err) {
      console.log('Oops, unable to copy',err);
   }    
   document.body.removeChild( textArea );
}

STEP 2: Give an id to your button and then add an event listener to it like this:

$( '#btnCopyToClipboard' ).click( function()
 {
     var clipboardText = "";
     clipboardText = $( '#txtKeyw' ).val(); 
     copyToClipboard( clipboardText );
     alert( "Copied to Clipboard" );
 });
like image 182
Virk Bilal Avatar answered Oct 26 '22 22:10

Virk Bilal


Try this..this is the correct way.

Step 1:

function copyToClipboard(text) {

   var textArea = document.createElement( "textarea" );
   textArea.value = text;
   document.body.appendChild( textArea );

   textArea.select();

   try {
      var successful = document.execCommand( 'copy' );
      var msg = successful ? 'successful' : 'unsuccessful';
      console.log('Copying text command was ' + msg);
   } catch (err) {
      console.log('Oops, unable to copy');
   }

   document.body.removeChild( textArea );
}

Step 2:

$( '#btnCopyToClipboard' ).click( function()
 {
     var clipboardText = "";

     clipboardText = $( '#txtKeyw' ).val();

     copyToClipboard( clipboardText );
     alert( "Copied to Clipboard" );
 });
like image 27
Jelson Lanto Avatar answered Oct 26 '22 23:10

Jelson Lanto