Results 1 to 4 of 4

Thread: getElementById with for loop

  1. #1
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default getElementById with for loop

    In an effort to string together cell info from multiple rows in a table, I'm wondering if it possible to use getElementById to accomplish this? My desired outcome of test code included is:
    AAA1
    BBB2

    I am attempting to work my way to AAA1AAA2BBB1BBB2 string that will be passed in a hidden field to a third party package. The sample code works when I limit to one row but errors when I add the for loop.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function showRows()
    {
    for (iii=0;iii<=1;iii++)
    {
    var x=document.getElementById('myTable').rows[iii].cells;
    
    document.write(x[0].innerHTML);
      document.write("<br />");
    }
    }
    </script>
    </head>
    <body>
     
    <table id="myTable" border="1">
    <tr>
    <td>AAA1</td>
    <td>AAA2</td>
    </tr>
    <tr>
    <td>BBB1</td>
    <td>BBB2</td>
    </tr>
    </table>
    <br />
    <input type="button" onclick="showRows()" value="Show rows in table">
    
    </body>
    </html>
    Last edited by djr33; 04-01-2010 at 10:19 PM. Reason: fixed [/code] tag

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Here:
    Code:
    <script type="text/javascript">
    var add = function(table, out){
      for(var i = 0; i < table.rows.length; i++){
        for(var a = 0; a < table.rows[i].cells.length; a++){
    	 out.value += table.rows[i].cells[a].innerHTML;
    	}
      }
    };
    </script>
    <input type="text" id="text" value="" />
    <table id="table">
      <tr><td>AAA1</td><td>AAA2</td></tr>
      <tr><td>BBB1</td><td>BBB2</td></tr>
    </table>
    <input type="button" onclick="add(document.getElementById('table'),document.getElementById('text'));" value="Add table up" />
    Jeremy | jfein.net

  3. #3
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Nile,

    That is a great example. Thank you for your help!

    Gary

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    No problem, glad to help.

    It seems your topic is solved... Please set the status to resolved.. To do this:
    Go to your first post ->
    Edit your first post ->
    Click "Go Advanced" ->
    Then in the drop down next to the title, select "RESOLVED"
    Jeremy | jfein.net

Bookmarks

Posting Permissions

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