Querying a dynamically constructed html table

U

ukrbend

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

oeb

ukrbend said:
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
 
T

Toby Inkster

ukrbend said:
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top