5

How to select (highlight) all text in table using JavaScript/jQuery like this?

https://s9.postimg.org/3str2tckf/Ask_Select_Table.png

<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>

Makyen
  • 31,849
  • 12
  • 86
  • 121
  • http://stackoverflow.com/questions/985272/selecting-text-in-an-element-akin-to-highlighting-with-your-mouse this can be your solution – mr. Aug 11 '16 at 06:07

1 Answers1

4

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

Community
  • 1
  • 1
Tony Vincent
  • 13,354
  • 7
  • 49
  • 68