5

Im just looking for advice on this. Ive been looking trough the internet for possible solutions on how to copy an HTML table structure with it's text to clipboard but no so lucky so far.

What I have at the moment is a simple table with data and users would need to copy that into an email using Outlook and when you copy/paste it. Pasting this manually into Outlook would show the table structure and its text rendered correctly. The only issue is that sometimes users could have several large tables making it sometimes clumsy to copy and scroll down at the same time to reach the bottom of the page.

So I am looking to get a simple button that essentially will do that automatically. So I am looking for something that would find my main div container and copy all of the table structures and text within it to the user's clipboard. I found that the most popular solution is called ZeroClipboard however it only copies the text and not the actual HTML table structure with it.

Would anyone know if this is something that is possible to accomplish with Jquery or other addons? I would appreciate any advice on this.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Daniel Ellison
  • 1,339
  • 4
  • 27
  • 49
  • Can you configure zero clipboard to copy a hidden input, then place the dom structure within the input? – Jason W Jun 01 '15 at 04:13
  • Going to come at this at a completely different angle: why would they need to copy/paste into an email client instead of simply telling your page(s) to just email them the currently highlighted range? (which your page tells your server about, and it then knows what to send the user for them to do with whatever they need?) – Mike 'Pomax' Kamermans Jun 01 '15 at 04:23
  • I thought about the option of having some sort of email template and have the HTML table automatically populated. I'm not the most savvy with jquery to do something like that but ill look into that possibility. – Daniel Ellison Jun 01 '15 at 04:27

2 Answers2

4

You could used also the execCommand method.

Example:

const btnCopyText = document.querySelector('#btn-copy-text');
const btnCopyTable = document.querySelector('#btn-copy-table');

const elText = document.querySelector('p');
const elTable = document.querySelector('table');

const copyEl = (elToBeCopied) => {
  let range, sel;

  // Ensure that range and selection are supported by the browsers
  if (document.createRange && window.getSelection) {

    range = document.createRange();
    sel = window.getSelection();
    // unselect any element in the page
    sel.removeAllRanges();

    try {
      range.selectNodeContents(elToBeCopied);
      sel.addRange(range);
    } catch (e) {
      range.selectNode(elToBeCopied);
      sel.addRange(range);
    }

    document.execCommand('copy');
  }

  sel.removeAllRanges();

  console.log('Element Copied! Paste it in a file')
};

btnCopyText.addEventListener('click', () => copyEl(elText));
btnCopyTable.addEventListener('click', () => copyEl(elTable));

HTML:

<div>
  <button id="btn-copy-text">Copy this text </button>
  <p id="text-to-copied">Text to be copied</p>
</div>

<div class="table-container">
  <button id="btn-copy-table">Copy Table</button>
  <table id="table-to-copied" border="1">
    <tr>
      <td>col1 </td>
      <td>col2 </td>
    </tr>
    <tr>
      <td>col1 </td>
      <td>col2 </td>
    </tr>
    <tr>
      <td>col1 </td>
      <td>col2 </td>
    </tr>
    <tr>
      <td>col1 </td>
      <td>col2 </td>
    </tr>
    <tr>
      <td>col1 </td>
      <td>col2 </td>
    </tr>
    <tr>
      <td>col1 </td>
      <td>col2 </td>
    </tr>
  </table>
</div>  

Live example: https://stackblitz.com/edit/js-copy-element

marianocodes
  • 794
  • 1
  • 10
  • 19
  • This solution seems to be working for all browsers. Thank you! But the question is ... how do I customize it to work with my code. I have a table with id (say #Table1) How will that affect the code. This javascript of yours is too advance for me to figure out anything :( – Aamir Aug 27 '19 at 14:04
  • @Aamir there is no code example in your question. Post a link with an example – marianocodes Aug 28 '19 at 00:22
3

I don't think you can trigger copy event with a button, but a suggestion for a workaround: clipboard API allows you to set custom data on copy event. So what you could do is listen for copy event on your table and send HTML as text instead. So a user triggering a copy event from your table would get the HTML (or whichever text you want) in his clipboard instead.

In snippet example below select some text and copy it:

document.getElementById('sample').addEventListener('copy', function (e) {
    var html_data = document.getElementById('sample').innerHTML;
  document.getElementById('result').textContent = html_data;
    e.clipboardData.setData('text/plain', html_data);
    e.preventDefault();
});
span
{
  color: red;
}
<div id='sample'>
    <div style="padding-bottom: 5px;">Select some of this text and copy it to clipboard using ctrl+c or right-click+copy.</div>
  
</div>
<div >The content of the clipboard is: <span id="result"></span></div>

Doc for clipboard API: http://www.w3.org/TR/clipboard-apis/

And from caniuse: http://caniuse.com/#feat=clipboard

Julien Grégoire
  • 16,864
  • 4
  • 32
  • 57
  • HI Julien thanks for the code, how would I trigger the copy from the button thought? Im a bit confused on how that would work. – Daniel Ellison Jun 01 '15 at 10:39
  • sorry i wasn't clear, see edit. I don't think a button is possible, it was a suggestion for a workaround. – Julien Grégoire Jun 01 '15 at 12:19
  • Thanks for the clarification, at the moment users are able to simply highlight the tables and right click copy or CTRL-C and paste it into an email just fine. I was really looking for a button that would copy the DIV contents automatically for the users. – Daniel Ellison Jun 01 '15 at 13:51
  • I understand users can copy now, what this solution does is allow you to push the content you want when they copy. They could select only a word or a letter and when they copy, they have the whole html of the table. Or you could have a different div altogether saying something like "copy this content to get the whole table" and when they do you push the whole table. In any case, it's a workaround, but maybe it doen't do what you need. – Julien Grégoire Jun 01 '15 at 14:19