1function selectElementContents(el) {
2 var body = document.body, range, sel;
3 if (document.createRange && window.getSelection) {
4 range = document.createRange();
5 sel = window.getSelection();
6 sel.removeAllRanges();
7 try {
8 range.selectNodeContents(el);
9 sel.addRange(range);
10 } catch (e) {
11 range.selectNode(el);
12 sel.addRange(range);
13 }
14 } else if (body.createTextRange) {
15 range = body.createTextRange();
16 range.moveToElementText(el);
17 range.select();
18 }
19}
1<table id="tableId" border="1">
2 <thead>
3 <tr><th>Heading 1</th><th>Heading 2</th></tr>
4 </thead>
5 <tbody>
6 <tr><td>cell 1</td><td>cell 2</td></tr>
7 </tbody>
8</table>
9
10<input type="button" value="select table" onclick="selectElementContents( document.getElementById('tableId') );">