Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17291
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Mattias Campe <mattiaspuntcampe@geeeemeil.com> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: getElementsByClassName: BUG in JS-engine? |
| Date | Sun, 18 Nov 2012 14:56:39 +0100 |
| Organization | A noiseless patient Spider |
| Lines | 24 |
| Message-ID | <50A8E917.3070303@geeeemeil.com> (permalink) |
| References | <k8aee8$54m$1@dont-email.me> <50a8c202$0$6565$9b4e6d93@newsspool4.arcor-online.net> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Info | mx04.eternal-september.org; posting-host="7a7c0a9f198479aa7eaac9257543584d"; logging-data="561"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1MIw6EOW3ik70zMM7IA8a" |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121028 Thunderbird/16.0.2 |
| In-Reply-To | <50a8c202$0$6565$9b4e6d93@newsspool4.arcor-online.net> |
| Cancel-Lock | sha1:n1RHOWQyRysLDWPk0aAm9e2PVTU= |
| Xref | csiph.com comp.lang.javascript:17291 |
Show key headers only | View raw
Op 18-11-12 12:09, Martin Honnen schreef:
> Most DOM collections are "live" collections so it is difficult iterating
> over them while manipulating them. Your loop manipulates the class of
> elements and that way the collection changes while the loop is executed
> and that leads to elements being skipped as after setting
> alleOpvallendeAlineas[0].className
> that element is removed from the collection and the one that was at
> index 1 is now at index 0. And the next loop iteration changes
> alleOpvallendeAlineas[1]
> which is the third element in the original collection.
>
> One way to avoid that problem is working from the end of the collection
> e.g.
> for (var l = alleOpvallendeAlineas.length, i = l - 1; i >= 0; i--) {
> alleOpvallendeAlineas[i].className = ...;
> }
I wouldn't have found that explanation myself, but it all makes sense,
once you know that :). So it's not a bug in the JS-engine after all ;).
Thank you very much for your fast and clear explanation!!
Kind regards
Mattias
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
getElementsByClassName: BUG in JS-engine? Mattias Campe <mattiaspuntcampe@geeeemeil.com> - 2012-11-18 11:47 +0100
Re: getElementsByClassName: BUG in JS-engine? Martin Honnen <mahotrash@yahoo.de> - 2012-11-18 12:09 +0100
Re: getElementsByClassName: BUG in JS-engine? Mattias Campe <mattiaspuntcampe@geeeemeil.com> - 2012-11-18 14:56 +0100
Re: getElementsByClassName: BUG in JS-engine? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-18 18:43 +0100
csiph-web