最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select the value of a TD on click to ease copy - Stack Overflow

programmeradmin6浏览0评论

I want to select onclick the innerHTML of a td so the user can just do ctrl+C to copy the content.

I tried many bination and I can't find a way. However it seams to work on inputs with a simple document.getElementById(id).select();

Adding a focus don't effect anything, and the .select() send and error:

document.getElementById(...).select is not a function

So how can I do with a td element? I don't mind if it's not working on IE.

Or if possible, directly copying the text.

I want to select onclick the innerHTML of a td so the user can just do ctrl+C to copy the content.

I tried many bination and I can't find a way. However it seams to work on inputs with a simple document.getElementById(id).select();

Adding a focus don't effect anything, and the .select() send and error:

document.getElementById(...).select is not a function

So how can I do with a td element? I don't mind if it's not working on IE.

Or if possible, directly copying the text.

Share Improve this question edited May 27, 2016 at 12:14 Valentin BEAULE asked May 27, 2016 at 8:14 Valentin BEAULEValentin BEAULE 4041 gold badge8 silver badges26 bronze badges 4
  • 1 stackoverflow./questions/1173194/… – ave4496 Commented May 27, 2016 at 8:17
  • I would use a little plugin such as timpietrusky./_select – Djave Commented May 27, 2016 at 8:17
  • @ValentinBEAULE You changed question to matching with marked answer? :) – Mohammad Commented May 27, 2016 at 12:25
  • Yes, so Viktor Koncsek get the reward he deserves. ^^ – Valentin BEAULE Commented May 27, 2016 at 13:11
Add a ment  | 

3 Answers 3

Reset to default 12

You can select text of td when it clicked.

$("td").click(function(){
    var range = document.createRange();
    range.selectNodeContents(this);  
    var sel = window.getSelection(); 
    sel.removeAllRanges(); 
    sel.addRange(range);
});
table, tr, td {
    border: 1px solid black;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr>
        <td>Column1</td>
        <td>Column2</td>
        <td>Column3</td>
    </tr>
    <tr>
        <td>Column1</td>
        <td>Column2</td>
        <td>Column3</td>
    </tr>
</table>

Also copying isn't that hard. I use this function, this works in other browsers too, not just IE (origin unknown).

https://jsfiddle/5bhkydq1/

html code

<div>
Click me to copy!
</div>

javascript and jquery

$('div').click(function(){
            copyTextToClipboard($(this).html());
});

function copyTextToClipboard(text) {
  var textArea = document.createElement("textarea");

  // Place in top-left corner of screen regardless of scroll position.
  textArea.style.position = 'fixed';
  textArea.style.top = 0;
  textArea.style.left = 0;

  // Ensure it has a small width and height. Setting to 1px / 1em
  // doesn't work as this gives a negative w/h on some browsers.
  textArea.style.width = '2em';
  textArea.style.height = '2em';

  // We don't need padding, reducing the size if it does flash render.
  textArea.style.padding = 0;

  // Clean up any borders.
  textArea.style.border = 'none';
  textArea.style.outline = 'none';
  textArea.style.boxShadow = 'none';

  // Avoid flash of white box if rendered for any reason.
  textArea.style.background = 'transparent';


  textArea.value = text;

  document.body.appendChild(textArea);

  textArea.select();

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

  document.body.removeChild(textArea);
}

try this:

$("td").click(function (e) {
    var clickedCell = $(e.target).closest("td");
    navigator.clipboard.writeText(clickedCell.text());
});

It will write the clicked cell text to the browser clipboard, works for me!

https://developer.mozilla/en-US/docs/Web/API/Clipboard/writeText

发布评论

评论列表(0)

  1. 暂无评论