How to select (highlight) all text in table using JavaScript/jQuery like this?
<table id="HighlightThis">
<thead>
<tr><th>Num.<th>Name
</thead>
<tbody>
<tr><td>1.<td>Cat
<tr><td>2.<td>Bird
<tr><td>3.<td>Fish
</tbody>
How to select (highlight) all text in table using JavaScript/jQuery like this?
<table id="HighlightThis">
<thead>
<tr><th>Num.<th>Name
</thead>
<tbody>
<tr><td>1.<td>Cat
<tr><td>2.<td>Bird
<tr><td>3.<td>Fish
</tbody>
Try this function should work in all mainstream browsers, credit Tim Down
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();
}
}
See the fiddle