I was wondering if anybody knows how to select using js the complete table, so that the user can right-click on the selection, copy it to the clipboard and then paste it on Excel. If you select the table manually, the process works perfectly. But sometimes, if the table height is a few times larger than the screen, selecting it dragging the mouse gets tedious. So, I want to give the users the possibility to click on a "select the whole table" button and everything gets ready to be copied.
Any ideas?
Yes. It's not too tricky, and the following will work in all mainstream browsers (including IE 6, and indeed 5):
(Updated 7 September 2012 after Jukka Korpela's comment pointing out that the previous version didn't work in IE 9 standards mode)
Demo: http://jsfiddle.net/timdown/hGkGp/749/
Code:
Here is what I have used. It also make the copy command so all you have to do is use the paste command in the document you want to place it into. Basically you wrap the content you want to copy in a div, grab it using innerHTML and copy it to the clipboard. I have not tested it on all browsers but it works in Chrome, Safari, Firefox.
Just to make the code proposed by Tim Down more complete, allowing de selected content to be automatically copied to clipboard:
I got it to work in IE9 finally by using the following script
NOTE: It doesn't work on html tables. It HAS to be a DIV. So just put a wrapper DIV around the table you need to select!
First I changed the HTML button code a bit:
Then changed the javascript to: