Querying a dynamically constructed html table

Discussion in 'HTML' started by ukrbend, Nov 29, 2005.

  1. ukrbend

    ukrbend Guest

    Is there a way to do a client side query (perhaps using a scripting
    language) that allows you to query info about an html table that was
    constructed on the server side via ASP? So for instance how many rows
    and columns the table has.

    Thanks for any help.
    ukrbend, Nov 29, 2005
    #1
    1. Advertising

  2. ukrbend

    oeb Guest

    ukrbend wrote:
    > Is there a way to do a client side query (perhaps using a scripting
    > language) that allows you to query info about an html table that was
    > constructed on the server side via ASP? So for instance how many rows
    > and columns the table has.
    >
    > Thanks for any help.
    >


    If you give the table a specific ID and onload count its child nodes
    (<tr> tags) using DOM, that should return the number of rows it has. and
    by counting the children of one of its child tags, you get the number of
    cols (the number of <td> in the first <tr>)

    Javascript would be very suitable for this, but I don't understand why
    you need to validate data like this on the client side. Can't you just
    do it on the server side where there is less to go wrong?

    oeb
    oeb, Nov 29, 2005
    #2
    1. Advertising

  3. ukrbend

    Toby Inkster Guest

    ukrbend wrote:

    > Is there a way to do a client side query (perhaps using a scripting
    > language) that allows you to query info about an html table that was
    > constructed on the server side via ASP? So for instance how many rows
    > and columns the table has.


    <table id="foo" border="1">
    <tr>
    <td>A</td>
    <td>B</td>
    <td>C</td>
    </tr>
    <tr>
    <td>Red</td>
    <td>Blue</td>
    <td>Green</td>
    </tr>
    <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    </tr>
    </table>

    <script type="text/javascript">
    // grab our table and its rows.
    var tabl = document.getElementById("foo");
    var rows = tabl.getElementsByTagName("TR");

    // count the number of rows
    var number_of_rows = rows.length;

    // columns is more tricky as each row may contain a
    // different number of cells!
    var number_of_cols = 0;
    for (var i=0; rows; i++)
    {
    var n = rows.getElementsByTagName("TD").length;
    if (n > number_of_cols) number_of_cols = n;
    }

    // report our results
    window.alert("foo has " + number_of_rows + " rows " +
    "and " + number_of_cols + " cols.");

    </script>

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
    Toby Inkster, Nov 29, 2005
    #3
  4. ukrbend

    ukrbend Guest

    Thanks Toby. Great code and exactly what I was looking for :}
    ukrbend, Nov 29, 2005
    #4
    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. Mike Cote
    Replies:
    1
    Views:
    315
    Ron Natalie
    Jan 22, 2004
  2. Pablo Gutierrez
    Replies:
    0
    Views:
    250
    Pablo Gutierrez
    Oct 3, 2003
  3. tabonni
    Replies:
    1
    Views:
    271
    Ray Costanzo [MVP]
    Oct 14, 2004
  4. Rasta
    Replies:
    2
    Views:
    131
    Rasta
    Aug 4, 2005
  5. T.G.
    Replies:
    2
    Views:
    1,046
Loading...

Share This Page