childElementCount work-around

J

joe

Firexfox and others but not ie 6 has childElementCount.
The code I have (snippet) is:
....
for (i=0;i<myelem.childElementCount;i++)
{
mysube=myelem.children;
....

Firefox runs ok, but ie6 does not have childElementCount. What would be the
simplest & compatible way to cycle through its children?
 
M

Martin Honnen

joe said:
Firexfox and others but not ie 6 has childElementCount.
The code I have (snippet) is:
...
for (i=0;i<myelem.childElementCount;i++)
{
mysube=myelem.children;
...

Firefox runs ok, but ie6 does not have childElementCount. What would be the
simplest& compatible way to cycle through its children?


Well if you write code against browsers supporting the "children"
collection then accessing "children.length" seems doable. But I don't
think Firefox supports the "children" collection at all in its versions
1.0 to 3.0.

So doing
for (var i = 0, l = myelem.childNodes.length; i < l; i++) {
if (myelem.childNodes.nodeType === 1) {
var mysubel = myelem.childNodes;
...
}
}
is certainly more portable than using "children".
 
T

Thomas 'PointedEars' Lahn

Martin said:
joe said:
Firexfox and others but not ie 6 has childElementCount.
The code I have (snippet) is:
...
for (i=0;i<myelem.childElementCount;i++)
{
mysube=myelem.children;
...

Firefox runs ok, but ie6 does not have childElementCount. What would be
the simplest& compatible way to cycle through its children?


Well if you write code against browsers supporting the "children"
collection then accessing "children.length" seems doable.


But inefficiently and potentially error-prone done here.

1. `i' should be declared.

2. The `childElementCount' property value should only be retrieved once (as
long as it does not change in the meantime).

3. Counting backwards reusing the value of `i--' could speed up matters even
more.
But I don't think Firefox supports the "children" collection at all in its
versions 1.0 to 3.0.

… which all are crawling with bugs and security leaks, having an estimated
steadily decreasing user base of at most 2% combined [1][2] (that's lower
than IE 7.0), and are long past their end-of-life (Firefox 3.0 on
2010-03-30, supplanted by auto-update with 3.6.13 [as I type], much the same
as 3.5 [3]), with no reasonable limits with regard to updates (by contrast
to, e.g., IE).

The more interesting question is which DOM implementations actually support
or are going to support the `children' and `childElementCount' properties,
given that HTML5 does not define them yet.


PointedEars
___________
[1] <http://gs.statcounter.com/#browser_version-ww-monthly-201005-201105>
[2] <http://netmarketshare.com/browser-market-share.aspx?spider=1&qprid=2>
[3]
<https://groups.google.com/forum/#!topic/mozilla.dev.planning/bktbwV57vBo>
 
M

Martin Honnen

Thomas said:
The more interesting question is which DOM implementations actually support
or are going to support the `children' and `childElementCount' properties,
given that HTML5 does not define them yet.

IE has had "children" in its HTML DOM since IE 4. And browsers like
Opera and Safari that way have "children" in their HTML DOM as well,
don't know since then.
As for HTML5 not defining that stuff, there is
http://www.w3.org/TR/ElementTraversal/ defining those properties and
that way I think it has found its way into Mozilla browsers.
Opera 11 seems to support "childElementCount" in both the XML and the
HTML DOM, Safari 5 however does not support it in the XML DOM, only the
HTML DOM.
 
D

Dr J R Stockton

In comp.lang.javascript message <c7d607d61s4nhh410ho3djr9mb6u56u9fr@4ax.
Firexfox and others but not ie 6 has childElementCount.
The code I have (snippet) is:
...
for (i=0;i<myelem.childElementCount;i++)
{
mysube=myelem.children;
...

Firefox runs ok, but ie6 does not have childElementCount. What would be the
simplest & compatible way to cycle through its children?



var KKK = myElem.firstChild
while (KKK) { /* use KKK ; */ KKK = KKK.nextSibling }

should work, after any required debugging, in any reasonably recent
browser.

To handle all descendant elements, as opposed to just child elements,
make that a function called with argument myElem which calls itself with
argument KKK either before or after the comment.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top