www.webdeveloper.com
Results 1 to 4 of 4

Thread: How can i count the number of columns in a table?

  1. #1
    Join Date
    Oct 2005
    Posts
    6

    How can i count the number of columns in a table?

    Hello People!

    I wanna find out the number of my columns in my table. How can I do that?
    I have tried to do it with the folowwing code, but it delivers me an "undifined" as result. What's wrong here?

    HTML Code:
    <html>
    <head>
    <title>Seitentitel</title>
    
    </head>
    <body>
    <table border="1" id="table1">
    <tr id="tr1">
    <td id="td1">erstes Feld</td>
    <td>zweites Feld</td>
    <tr>
    </table>
    <script language="javascript">
    	var wi = document.getElementById("table1").length;
    	document.write(wi);	
    </script>
    </body>
    <html>

  2. #2
    Join Date
    Sep 2005
    Posts
    40
    I think this code block will help. I used it to change the background colors of some table cells.

    //reset cells to be same background color as table they are in
    for (rowCounter = 0; rowCounter < 3; rowCounter++)
    {
    for (colCounter = 0; colCounter < 5; colCounter += 2)
    {
    document.getElementById("optionsTable").rows[8 + rowCounter].cells[colCounter].style.backgroundColor = document.getElementById("optionsTable").style.backgroundColor;
    }
    }

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    if no colSpan, the number of the columns is equal with the number of the cells of any row, let's say the first row

    var nrcols = document.getElementById('table1').getElementsByTagName('tr')[0].getElementsByTagName('td').length

  4. #4
    Join Date
    Nov 2011
    Posts
    1
    assuming that every row has the same number of columns then just count the number of columns on the first row:

    Code:
    cols = document.getElementById('tablename').rows[0].cells.length;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles