Problem with getting nested tables

J

Joseph Scoccimaro

I am currently working on a project for school dealing with accessing
the DOM. I am trying to get access to inner tables that are nested with
in other tables. Currently I am able to get all parent tables without a
problem. When I try to get an inner table through the child nodes it
says that the tr elements of the parent table are undefined. Posted is
the code I am using:

function nestedTableAnalysis()
{



DocBodyChildren = document.body.childNodes;
ParentTableTags = new Array();
i = 0;

for(j=0; j< DocBodyChildren.length; j++) {
if(DocBodyChildren[j].nodeName.toLowerCase() == \"table\") {
ParentTableTags = DocBodyChildren[j];
i++;
}//end if

}


for(k = 0; k < ParentTableTags.length; k++ ) {

childTableAnalysis(ParentTableTags[k].childNodes);

}


}//end nestedTableAnalysis()



function childTableAnalysis(NodesToCheck, tabSpace, periodSpacer)
{



j = 1;

for(i=0; i < NodesToCheck.length; i++) {

document.writeln(NodesToCheck.nodeName);

if(NodesToCheck.nodeName == \"TABLE\") {
document.writeln(\"<p> Table Found!</p>\");


j++;

childTableAnalysis(NodesToCheck.childNodes);
}//end if

if(NodesToCheck.nodeName == \"TR\") {
childTableAnalysis(NodesToCheck.childNodes);
}

if(NodesToCheck.nodeName == \"TD\") {
childTableAnalysis(NodesToCheck.childNodes);
}


}

}//end childTableAnalysis

Thanks,
Joseph Scoccimaro
(e-mail address removed)
 
M

Martin Honnen

Joseph said:
I am trying to get access to inner tables that are nested with
in other tables. Currently I am able to get all parent tables without a
problem. When I try to get an inner table through the child nodes it
says that the tr elements of the parent table are undefined.

HTML 4 wants all table elements to have at least one explicit or
implicit tbody child element and all tr elements are then children of a
thead or tfoot or tbody but not direct children of the table itself. In
the HTML DOM a table element object has a property named tBodies which
is a collection of the explicit and implicit tbody child elements. Use
that instead of walking childNodes and looking for elements with a
certain tagName.
<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-64060425>
You can also use the rows collection property of table or tbody or thead
or tfoot elements:
<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-67417573>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top