1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

javascript iterating through table

Discussion in 'HTML & Website Design' started by DevDream, Dec 1, 2016.

  1. #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>
    Code (markup):

    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

     
    Last edited by a moderator: Dec 3, 2016
    DevDream, Dec 1, 2016 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    1) You should really NEVER use innerHTML. It's outdated, gets the parser involved, in cases like this could trigger reverse parsing. It's an outdated, outmoded, halfwit inept way of dealing markup.

    2) Even if you WERE to use it, you're not indexing ROWS properly. You forgot to index by i

    
    var one = table.rows[i].cells[1].innerHTML;
    
    Code (markup):
    Really though, I would do this using the DOM since usually if it's a table you'd have a proper THEAD, and such routines you would only want to parse the TR inside TBODY.
     
    deathshadow, Dec 3, 2016 IP
  3. DevDream

    DevDream Member

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    sorry im a beginner, could you provide an example please?
     
    DevDream, Dec 10, 2016 IP