Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

childNodes in Mozilla 1

Status
Not open for further replies.

EwS

Programmer
Dec 30, 2002
398
US
I need to get the number of rows in my table
Code:
allTR = document.getElementById("table").childNodes[0].childNodes;
totalRows = allTR.length;

and, for each row, retrieve
Code:
allTR[x].childNodes[col].innerText

totalRows returns the correct number in IE, but in Mozilla, Opera, Netscape, Firefox, and Safari it returns 0. How can I fix it? Thank you!
 
allTr looks like it returns all of the cells in row 0:

Code:
allTR = document.getElementById("table").childNodes[0].childNodes;
[red]^^ var   ^^ doc    ^^ table ^^            ^^ first child(row)  ^^ cells[/red]

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Maybe it looks like it, but in IE allTR holds all the rows in the table. How can I do the same in other browsers?
 
Why that happens... some browsers put textNode objects (node type 3) for every whitespace inside a table, so childNodes[0] returns this node instead of TBODY. IE ignores these nodes, Mozilla doesn't. Because of that it is recommended to use collections tBodies, rows and cols instead of childNodes. For example:
Code:
oTab.tBodies[0].rows.length;
 
This is how I retrieve the value of a particular cell:

Code:
oTab.tBodies.rows[0].cells[0].firstChild.nodeValue

How to retrieve the value of the whole row? - Thanks.
 
This returns value of first element in particular cell.

What is the value of entire row? HTML code? If true, then try .rows[0].innerHTML.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top