javascript iterate through table

Discussion in 'Web Design and Development' started by mbpowner, Dec 1, 2016.

  1. mbpowner macrumors regular

    Joined:
    Aug 19, 2016
    #1
    <!DOCTYPE html>
    <html>

    <head>
    <title>test</title>
    <script>
    var table=document.getElementById("myTable");
    for (var i = 0, i < table.rows.length; i++)
    {
    var one=(table.rows.cells[1].innerHTML);
    document.write(one);
    }

    </script>
    </head>

    <body>

    <table border="1" id="myTable">

    <tr>
    <td>a</td>
    <td>b</td>
    <td>c</td>
    </tr>

    <tr>
    <td>d</td>
    <td>e</td>
    <td>f</td>
    </tr>

    <tr>
    <td>g</td>
    <td>h</td>
    <td>i</td>
    </tr>

    </table>


    </body>

    </html>

    this is just a test to see whether tables can be iterated through using this method, however no output is produced using the javascript code. can anyone suggest why? please thanks
     
  2. 960design, Dec 3, 2016
    Last edited: Dec 3, 2016

    960design macrumors 68020

    Joined:
    Apr 17, 2012
    Location:
    Destin, FL
    #2
    Looks like you made a couple of mistakes:
    1. <script> in header is running before body exists... so a null return
    2. comma in for loop, instead of a semicolon
    3. missed adding the index to the row[]
    Check the JSBIN ( remember to put the <script> tags right at the bottom of code, right above the closing </body> tag
    https://jsbin.com/qotohofije/edit?html,js,console

    PS. looks like I forgot to iterate the iterator... can you fix it?
     

Share This Page