Copy from table td to clipboard doesn't work


Besenyi

Can you take a look at this demo and let me know why I can't copy the previous text tdafter clicking it .btn-copy?

$('.btn-copy').on('click', function(){
$(this).closest('td').prev('td').select();
    try {
       var successful = document.execCommand('copy');
      if(successful) {
        $('.res').html("Coppied");
      }
       else
       { $('.res').html("Unable to copy!");} 
   } catch (err) {
      $('.res').html(err);
   }
});
table{border:1px solid #ccc;}
td{border:1px solid #ccc;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="res"></div>
<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
    <th>Copy</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td><button class="btn btn-default btn-copy">Copy</button></td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
    <td><button class="btn btn-default btn-copy">Copy</button></td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td><button class="btn btn-default btn-copy">Copy</button></td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td><button class="btn btn-default btn-copy">Copy</button></td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
    <td><button class="btn btn-default btn-copy">Copy</button></td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td><button class="btn btn-default btn-copy">Copy</button></td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>

Neil

You can still use document.execCommand('copy');, but your text selection is incorrect, this should fix it:

$('.btn-copy').on('click', function(){
  element = $(this).closest('td').prev('td')[0];
  var selection = window.getSelection();        
  var range = document.createRange();
  range.selectNodeContents(element);
  selection.removeAllRanges();
  selection.addRange(range);
  //Losely basd on http://stackoverflow.com/a/40734974/7668911
    try {
       var successful = document.execCommand('copy');
      if(successful) {
        $('.res').html("Coppied");
      }
       else
       { $('.res').html("Unable to copy!");} 
   } catch (err) {
      $('.res').html(err);
   }
});
table{border:1px solid #ccc;}
td{border:1px solid #ccc;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="res"></div>
<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
    <th>Copy</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td><button class="btn btn-default btn-copy">Copy</button></td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
    <td><button class="btn btn-default btn-copy">Copy</button></td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td><button class="btn btn-default btn-copy">Copy</button></td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td><button class="btn btn-default btn-copy">Copy</button></td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
    <td><button class="btn btn-default btn-copy">Copy</button></td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td><button class="btn btn-default btn-copy">Copy</button></td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>

Related


Copy from table td to clipboard doesn't work

Besenyi Can you take a look at this demo and let me know why I can't copy the previous text tdafter clicking it .btn-copy? $('.btn-copy').on('click', function(){ $(this).closest('td').prev('td').select(); try { var successful = document.execCommand(

Copy from table td to clipboard doesn't work

Besenyi Can you take a look at this demo and let me know why I can't copy the previous text tdafter clicking it .btn-copy? $('.btn-copy').on('click', function(){ $(this).closest('td').prev('td').select(); try { var successful = document.execCommand(

Copy from table td to clipboard doesn't work

Besenyi Can you take a look at this demo and let me know why I can't copy the previous text tdafter clicking it .btn-copy? $('.btn-copy').on('click', function(){ $(this).closest('td').prev('td').select(); try { var successful = document.execCommand(

Copy from table td to clipboard doesn't work

Besenyi Can you take a look at this demo and let me know why I can't copy the previous text tdafter clicking it .btn-copy? $('.btn-copy').on('click', function(){ $(this).closest('td').prev('td').select(); try { var successful = document.execCommand(

Copy from table td to clipboard doesn't work

Besenyi Can you take a look at this demo and let me know why I can't copy the previous text tdafter clicking it .btn-copy? $('.btn-copy').on('click', function(){ $(this).closest('td').prev('td').select(); try { var successful = document.execCommand(

JavaScript copy to clipboard doesn't work

User7570296: I have a function in my script that gives me an error. The purpose of this function is to copy text from a static panel (not a textbox or input) via the onClick event. Uncaught TypeError: copyText.select is not a function What I want is to enable

Copy to clipboard doesn't work javascript

Christian Poland var $temp = $("<input>"); $("body").append($temp); $temp.val('123').select(); document.execCommand("copy"); $temp.remove(); What's wrong with this code? should be correct Doesn't work at all for me. I've read a lot of topics here and this see

JavaScript copy to clipboard doesn't work

User7570296: I have a function in my script that gives me an error. The purpose of this function is to copy text from a static panel (not a textbox or input) via the onClick event. Uncaught TypeError: copyText.select is not a function What I want is to enable

HTML copy to clipboard button doesn't work

Oscar I'm trying to copy a link ( www.google.com ) to the clipboard using a button, but it doesn't work. My code is: <script type='text/javascript'> function myFunction() { /* Get the text field */ var copyText = document.getElementById("www.go

Copy to clipboard doesn't work on Chrome in VueJS

Vinavi Anutara In my VueJS app, I have a component that copies the base URL to the clipboard by clicking a link <a @click="copyURL" ref="mylink"> <img class="social_icon" src="/images/game/copy-fr.png" alt="Copy icon" /></a> <in

clipboard doesn't work

Daniellerondiyev I use Arch Linux (4.10.13-1-ARCH). Xfce environment. A few days ago I thought the clipboard was not working properly on Google Chrome, Onlyoffice, Okular, Gimp. It still works in a similar way: Termianl <-> Mouse Pad, Terminal <-> Terminal, Mo

clipboard doesn't work

Daniellerondiyev I use Arch Linux (4.10.13-1-ARCH). Xfce environment. A few days ago I thought the clipboard was not working properly on Google Chrome, Onlyoffice, Okular, Gimp. It still works in a similar way: Termianl <-> Mouse Pad, Terminal <-> Terminal, Mo

Copy to global clipboard doesn't work with Java in Ubuntu

Carousel: The following code from a standalone app works in ubuntu: import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Unsupported

ng-clip copy to clipboard doesn't work

Bharat I am using some tutorials to implement ng-clip. I'm following the tutorial and it's not working. I include Zeroclipboard.min.js, angular.js, ngClip.js HTML looks like.. <div ng-app="clip"> <button clip-copy="getTextToCopy()">Copy</button>

Copy to global clipboard doesn't work with Java in Ubuntu

Carousel: The following code from a standalone app works in ubuntu: import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Unsupported

ng-clip copy to clipboard doesn't work

Bharat I am using some tutorials to implement ng-clip. I'm following the tutorial and it's not working. I include Zeroclipboard.min.js, angular.js, ngClip.js HTML looks like.. <div ng-app="clip"> <button clip-copy="getTextToCopy()">Copy</button>

ng-clip copy to clipboard doesn't work

Bharat I am using some tutorials to implement ng-clip. I'm following the tutorial and it's not working. I include Zeroclipboard.min.js, angular.js, ngClip.js HTML looks like.. <div ng-app="clip"> <button clip-copy="getTextToCopy()">Copy</button>

C# copy to clipboard doesn't work at runtime

Samar I am using asp.net and c# on a web application in code behind. I just want to copy the text in the textbox to the clipboard, my code is as follows: The problem is, this works fine in debug mode, but when deploying my website, in run mode, CopyToClipboard

JavaScript copy textarea value to clipboard doesn't work

user 678976 I'm trying to copy the content of a textarea to the clipboard using javascript, but it doesn't work. When I check the clipboard it is empty. My Javascript is: function copyStudentEmails() { /* Get the text field */ var copyStudentEmail = docume

Copy to global clipboard doesn't work with Java in Ubuntu

Carousel: The following code from a standalone app works in ubuntu: import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Unsupported

ng-clip copy to clipboard doesn't work

Bharat I am using some tutorials to implement ng-clip. I'm following the tutorial and it's not working. I include Zeroclipboard.min.js, angular.js, ngClip.js HTML looks like.. <div ng-app="clip"> <button clip-copy="getTextToCopy()">Copy</button>

C# copy to clipboard doesn't work at runtime

Samar I am using asp.net and c# on a web application in code behind. I just want to copy the text in the textbox to the clipboard, my code is as follows: The problem is, this works fine in debug mode, but when deploying my website, in run mode, CopyToClipboard

Table TD width doesn't work

Ile Popivanov I'm making a dropdown and apparently I can't change the width trying other methods. I want each column to be 200px wide but it doesn't work. .dropbtn { background-image: url("../sliki/meni.png"); width: 220px; height: 60px; border: none;