469,291 Members | 1,837 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,291 developers. It's quick & easy.

Alternative to "children" property of DOM in firefox

I have the following code

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

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

Regards,
Angel

Jun 19 '06 #1
10 11067
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[i].children.length)

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


alert(thisDoc.rows[i].cells.length)

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 19 '06 #2
Angel wrote:
I have the following code

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

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

Regards,
Angel


What about alert(thisDoc.childNodes[i].childNodes.length) ?
And I would expect it to work in IE6 too.
Jun 19 '06 #3
Robert wrote:
Angel wrote:
I have the following code

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

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

Regards,
Angel


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


Only if thisDoc.childNodes[i] is not a #text node. It will most likely
be a tableSection element - thead, tfoot or tbody - or maybe a caption,
col or colgroup.
--
Rob
Jun 19 '06 #4
Hi,

RobG wrote:
Robert wrote:
Angel wrote:
I have the following code

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

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

Regards,
Angel


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


Only if thisDoc.childNodes[i] 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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Jun 19 '06 #5
Laurent Bugnion wrote:
Hi,

RobG wrote:
Robert wrote:
Angel wrote:
I have the following code

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

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

Regards,
Angel
What about alert(thisDoc.childNodes[i].childNodes.length) ?
And I would expect it to work in IE6 too.


Only if thisDoc.childNodes[i] 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[i])
&& (o = o.childNodes) )
{
alert(o.length);
}
:-)

--
Rob
Jun 19 '06 #6
I'm not sure this is what you need, but I use in my code something like:

thisDoc.childNodes[i].childNodes.length

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

------
William
Angel wrote:
I have the following code

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

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

Regards,
Angel

Jun 19 '06 #7
RobG wrote:
Laurent Bugnion wrote:
Hi,

RobG wrote:
Robert wrote:

Angel wrote:

> var thisDoc=document.getElementById("myTable"); // myTable is the name
> of the table
> alert(thisDoc.childNodes[i].children.length)
>
> How do I change the second line of code for it to work in firefox?

What about alert(thisDoc.childNodes[i].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.
Jun 19 '06 #8
Robert wrote:
RobG wrote:
> 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>

--
Rob

Jun 20 '06 #9
RobG wrote:
Robert wrote:
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>


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.
Jun 20 '06 #10
Robert wrote:
RobG wrote:
Robert wrote:
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>


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


I think this is the thread that I found which made me change my javascript.
http://groups.google.com/group/micro...8675fc965b0500

"I was mixing DOM calls with table object model enumerations. Using only
DOM fixed it."
Jun 20 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

24 posts views Thread by Charles Crume | last post: by
1 post views Thread by Todd Cary | last post: by
reply views Thread by Tim Mostad | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.