Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

childElementCount work-around

32 views
Skip to first unread message

joe

unread,
Jun 23, 2011, 8:47:18 AM6/23/11
to
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[i];
...

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

Martin Honnen

unread,
Jun 23, 2011, 8:54:39 AM6/23/11
to
joe wrote:
> 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[i];
> ...
>
> 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[i].nodeType === 1) {
var mysubel = myelem.childNodes[i];
...
}
}
is certainly more portable than using "children".


--

Martin Honnen --- MVP Data Platform Development
http://msmvps.com/blogs/martin_honnen/

Thomas 'PointedEars' Lahn

unread,
Jun 23, 2011, 9:46:20 AM6/23/11
to
Martin Honnen wrote:

> joe wrote:
>> 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[i];
>> ...
>>
>> 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>
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300...@news.demon.co.uk>

Martin Honnen

unread,
Jun 23, 2011, 12:24:26 PM6/23/11
to
Thomas 'PointedEars' Lahn wrote:

> 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.

Dr J R Stockton

unread,
Jun 24, 2011, 6:23:06 PM6/24/11
to
In comp.lang.javascript message <c7d607d61s4nhh410ho3djr9mb6u56u9fr@4ax.
com>, Thu, 23 Jun 2011 15:47:18, joe <m...@invalid.com> posted:


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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Eric Bednarz

unread,
Jun 24, 2011, 8:13:42 PM6/24/11
to
Dr J R Stockton <repl...@merlyn.demon.co.uk> writes:

> var KKK = myElem.firstChild

Pardon my french, but shouldn’t that read

var NSDAP = myElem.firstChild

?

0 new messages