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:
function selectElementContents(el) {
var body = document.body, range, sel;
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
sel.removeAllRanges();
try {
range.selectNodeContents(el);
sel.addRange(range);
} catch (e) {
range.selectNode(el);
sel.addRange(range);
}
} else if (body.createTextRange) {
range = body.createTextRange();
range.moveToElementText(el);
range.select();
}
}
<table id="tableId" border="1">
<thead>
<tr><th>Heading 1</th><th>Heading 2</th></tr>
</thead>
<tbody>
<tr><td>cell 1</td><td>cell 2</td></tr>
</tbody>
</table>
<input type="button" value="select table" onclick="selectElementContents( document.getElementById('tableId') );">