Highlighting / Selecting columns in an HTML table

Discussion in 'Javascript' started by karflips33@fastmail.fm, Oct 10, 2006.

  1. Guest

    A user of mine wanted to copy and paste single columns from an existing
    HTML table in a webpage into an excel spreadsheet. You can't do this by
    clicking and dragging the mouse over a column - it selects the whole
    html table.

    I worked up the following code and thought someone else might benefit.
    My approach involves changing the table structure slightly. (click on
    column headers to select the row):

    <html>
    <head>
    <script>
    function ControlRangeSelect(intColumn, intRows) {

    if (intRows > 0)
    {

    for(var i=1;i<=intRows;i++)
    {
    eval("var r" + i + " = document.body.createTextRange()")
    eval("r" + i + ".moveToElementText(document.getElementById('div" +
    intColumn + i + "'))");
    }

    eval("r1.setEndPoint('EndToEnd', r" + intRows + ")");
    r1.select();

    }
    }

    </script>
    </head>

    <body>
    <table>
    <td>
    <table>
    <tr><td onClick="ControlRangeSelect(1,3);">col1</td>
    <tr><td><div id=div11>col1</div></td>
    <tr><td><div id=div12>col1</div></td>
    <tr><td><div id=div13>col1</div></td>
    </table>
    </td>

    <td>
    <table>
    <tr><td onClick="ControlRangeSelect(2,3);">col2</td>
    <tr><td><div id=div21>col2</div></td>
    <tr><td><div id=div22>col2</div></td>
    <tr><td><div id=div23>col2</div></td>
    </table>
    </td>

    <td>
    <table>
    <tr><td onClick="ControlRangeSelect(3,3);">col3</td>
    <tr><td><div id=div31>col3</div></td>
    <tr><td><div id=div32>col3</div></td>
    <tr><td><div id=div33>col3</div></td>
    </table>
    </td>

    <td>
    <table>
    <tr><td onClick="ControlRangeSelect(4,3);">col4</td>
    <tr><td><div id=div41>col4</div></td>
    <tr><td><div id=div42>col4</div></td>
    <tr><td><div id=div43>col4</div></td>
    </table>
    </td>

    </table>
    </body>
    </html>
     
    , Oct 10, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Loveshade
    Replies:
    1
    Views:
    7,680
    Venkat_KL
    Nov 19, 2005
  2. .NETn00b
    Replies:
    0
    Views:
    504
    .NETn00b
    Nov 19, 2005
  3. JMC
    Replies:
    0
    Views:
    444
  4. yawnmoth
    Replies:
    1
    Views:
    714
    Martin Honnen
    May 25, 2009
  5. .NETn00b

    Selecting and Highlighting Multiple Rows in a DataGrid

    .NETn00b, Nov 19, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    128
    .NETn00b
    Nov 19, 2005
Loading...

Share This Page