Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7063539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:43:32+00:00 2026-05-28T04:43:32+00:00

I’m doing some manipulation and retrieval of values stored in a table. Each row

  • 0

I’m doing some manipulation and retrieval of values stored in a table.

Each row contains record where each column represents a property of that record. Much like how you typically think of a database table.

So my first question is: Is it a safe/acceptable/portable practice to access elements in the row using known childNodes[] indices of the row?

Given this example table:

<table id='sometable'>
    <thead><tr><th>Name</th> <th>Age</th></tr></thead>
    <tbody>
       <tr><td>Bob</td><td>25</td></tr>
       <tr><td>Alice</td><td>31</td></tr>
    </tbody>
</table>

Is it ok to do things like this?

  /* Get just the 'tbody' rows, i.e. not the header row */
  function getTableBodyRows(tblName){
    return document.getElementById(tblName).getElementsByTagName('tbody')[0].getElementsByTagName('tr');
  }

  /* Get the 'name' value for each row */
  function getNames( ){
     var rows = getTableBodyRows('sometable');
     var names = [];
     for( var i=0; i < rows.length; i++ ){
         /* This line assumes childNode[0] of the row is the 'name' cell (td) which
            contains only a single text node.  */
         names.push( rows[i].childNodes[0].childNodes[0].nodeValue);
     }
     return names;
  }

Aside from the fact that I would probably define a value to represent the column numbers instead of using literal is there anything wrong with this approach?

One alternative I thought of would be to add a class attribute to each <td> to identify the content of the cell, and then use a getElementsByClassName() method to retrieve individual values, but it seems like performance would take a significant hit for large tables. And result in writing more code for little benefit.

Are there other common techniques for accomplishing the tasks like “get the ‘name’ value from table row ‘x'”?

I should also note that I am generating my tables with javascript so I don’t think I have to worry about the issue of how different browsers may or may not consider whitespace a childNode.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:43:32+00:00Added an answer on May 28, 2026 at 4:43 am

    “Is it a safe/acceptable/portable practice to access elements in the row using known childNodes[] indices of the row?”

    If by elements, you mean just the cells, then no. You should use the .cells property of the row to avoid potential whitespace between cells.

    I’d update your code like this:

      function getNames( ){
         var rows = document.getElementById('sometable').tBodies[0].rows;
         var names = [];
         for( var i=0; i < rows.length; i++ ){
             names.push( rows[i].cells[0].childNodes[0].nodeValue);
         }
         return names;
      }
    

    Notice that I’ve also used the .tBodies property of the table, and the .rows property of the tbody.

    You can also use the .rows property directly on the table to get all rows in the table.


    I missed your last sentence about whitespace, but I’d still use the built-in collections that tables provide instead of DOM selection methods.

    No sense in building new collections when they’re right there waiting for you. 😉

    While childNodes would work when there’s no whitespace, it would be a better habit to use .cells.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.