salmonmoose salmonmoose - 14 days ago 5
HTML Question

Highlight all data in a HTML Table

Is there a way to do a selection highlight over all elements in a HTML Table?

I want to be able to have data displayed in a tabular form, and the user to be able to click a button to select the relevant data to copy and paste into the spreadsheet of their choosing.

Answer

Try this:

function selectAll(parentNode) {
    var sel;
    // IE
    if (document.selection) {
        sel = document.body.createTextRange();
        sel.moveToElementText(parentNode);
        sel.select();
    } else {
        sel = document.createRange();
        sel.setStartBefore(parentNode);
        sel.setEndAfter(parentNode);
        window.getSelection().addRange(sel);
    }
}

selectAll(document.getElementById('myTable'));

Test it out here.

Comments