Alternative to "children" property of DOM in firefox

A

Angel

I have the following code

var thisDoc=document.getElementById("myTable"); // myTable is the name
of the table
alert(thisDoc.childNodes.children.length)

How do I change the second line of code for it to work in firefox?

Regards,
Angel
 
E

Evertjan.

Angel wrote on 19 jun 2006 in comp.lang.javascript:
I have the following code

var thisDoc=document.getElementById("myTable"); // myTable is the name
of the table
alert(thisDoc.childNodes.children.length)

How do I change the second line of code for it to work in firefox?


alert(thisDoc.rows.cells.length)

[only partly tested and me not knowing much about FF,
it seems the natural way to DOMinate]
 
R

Robert

Angel said:
I have the following code

var thisDoc=document.getElementById("myTable"); // myTable is the name
of the table
alert(thisDoc.childNodes.children.length)

How do I change the second line of code for it to work in firefox?

Regards,
Angel


What about alert(thisDoc.childNodes.childNodes.length) ?
And I would expect it to work in IE6 too.
 
R

RobG

Robert said:
Angel said:
I have the following code

var thisDoc=document.getElementById("myTable"); // myTable is the name
of the table
alert(thisDoc.childNodes.children.length)

How do I change the second line of code for it to work in firefox?

Regards,
Angel


What about alert(thisDoc.childNodes.childNodes.length) ?
And I would expect it to work in IE6 too.


Only if thisDoc.childNodes is not a #text node. It will most likely
be a tableSection element - thead, tfoot or tbody - or maybe a caption,
col or colgroup.
 
L

Laurent Bugnion

Hi,
Robert said:
Angel said:
I have the following code

var thisDoc=document.getElementById("myTable"); // myTable is the name
of the table
alert(thisDoc.childNodes.children.length)

How do I change the second line of code for it to work in firefox?

Regards,
Angel


What about alert(thisDoc.childNodes.childNodes.length) ?
And I would expect it to work in IE6 too.


Only if thisDoc.childNodes is not a #text node. It will most likely
be a tableSection element - thead, tfoot or tbody - or maybe a caption,
col or colgroup.


That puzzled me too, but then I thought, the problem will be the same
with children, so hopefully the OP knows what he does.

To handle the issue of the text node, I would introduce some simple
checks like this:

if ( thisDoc
&& thisDoc.childNodes
&& thisDoc.childNodes[ i ]
&& thisDoc.childNodes[ i ].childNodes )
{
alert( thisDoc.childNodes[ i ].childNodes.length );
}

HTH,
Laurent
 
R

RobG

Laurent said:
Hi,
Robert said:
Angel wrote:
I have the following code

var thisDoc=document.getElementById("myTable"); // myTable is the name
of the table
alert(thisDoc.childNodes.children.length)

How do I change the second line of code for it to work in firefox?

Regards,
Angel


What about alert(thisDoc.childNodes.childNodes.length) ?
And I would expect it to work in IE6 too.


Only if thisDoc.childNodes is not a #text node. It will most
likely be a tableSection element - thead, tfoot or tbody - or maybe a
caption, col or colgroup.


That puzzled me too, but then I thought, the problem will be the same
with children, so hopefully the OP knows what he does.

To handle the issue of the text node, I would introduce some simple
checks like this:

if ( thisDoc
&& thisDoc.childNodes
&& thisDoc.childNodes[ i ]
&& thisDoc.childNodes[ i ].childNodes )
{
alert( thisDoc.childNodes[ i ].childNodes.length );
}


That seems rather a lot of work when it is likely that the OP is after
the table's rows collection, in which case Evertjan's reply fits the
bill: thisDoc.rows.length.

The above sequential test, if it was necessary, could be more efficient as:

var o;
if ( thisDoc
&& (o = thisDoc.childNodes)
&& (o = o)
&& (o = o.childNodes) )
{
alert(o.length);
}
:)
 
W

William

I'm not sure this is what you need, but I use in my code something like:

thisDoc.childNodes.childNodes.length

and it works pretty well with Firefox, but I would verify if it has
children first with childNodes.hasChildren I think.
 
R

Robert

RobG said:
Laurent said:
Hi,
Robert wrote:

Angel wrote:

var thisDoc=document.getElementById("myTable"); // myTable is the name
of the table
alert(thisDoc.childNodes.children.length)

How do I change the second line of code for it to work in firefox?

What about alert(thisDoc.childNodes.childNodes.length) ?
And I would expect it to work in IE6 too.

To handle the issue of the text node, I would introduce some simple
checks like this:

if ( thisDoc
&& thisDoc.childNodes
&& thisDoc.childNodes[ i ]
&& thisDoc.childNodes[ i ].childNodes )
{
alert( thisDoc.childNodes[ i ].childNodes.length );
}



That seems rather a lot of work when it is likely that the OP is after
the table's rows collection, in which case Evertjan's reply fits the
bill: thisDoc.rows.length.


I have to give a warning. IE6 can give a "Pure Virtual Call" error and
crash when mixing Core DOM methods and HTML Table DOM methods.
 
R

RobG

Robert said:
RobG said:
Angel wrote:

var thisDoc=document.getElementById("myTable"); // myTable is the name
[...]
That seems rather a lot of work when it is likely that the OP is after
the table's rows collection, in which case Evertjan's reply fits the
bill: thisDoc.rows.length.

I have to give a warning. IE6 can give a "Pure Virtual Call" error and
crash when mixing Core DOM methods and HTML Table DOM methods.

I'd expect that if that's true then it would have been mentioned
here before, do you have an example? As far as I can determine, it
hasn't. Microsoft themselves recommend using the DOM HTML Table
interface for manipulating tables:

<URL; http://msdn.microsoft.com/workshop/author/tables/buildtables.asp>


Note: their documentation for insertRow has so many errors I wouldn't
be surprised if they've got it completely backwards: :-(

<URL:
http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/insertrow.asp>
 
R

Robert

RobG said:
I'd expect that if that's true then it would have been mentioned
here before, do you have an example? As far as I can determine, it
hasn't. Microsoft themselves recommend using the DOM HTML Table
interface for manipulating tables:

<URL; http://msdn.microsoft.com/workshop/author/tables/buildtables.asp>

I was confronted by this problem and saw IE6 crash consistently (tried
many computers) for no apparent reason. After debugging I traced the
problem to a DOM call. There was no reason for it to crash. I googled
and someone mentioned that this could happen when mixing those different
DOM methods. I had nothing to loose so I replaced the HTML Table DOM
Methods with Core DOM methods. I remember especially that I changed
using the rows and cells collection, so then doing it the hard way by
stepping through the childNodes. After this change the problems were gone.

If I find this google reference or the code I changed in CVS maybe I can
tell more.
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top