By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,472 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,472 IT Pros & Developers. It's quick & easy.

for (.. in ..) not working

P: n/a
Hello.
Some days ago I posted a message about this (somebody replied asking me
if I was using IE, but I am using Mozilla 1.7.5):

This 'for' statment works for me, i.e.:

for (var i = aElts.length; i--; ){
var oElement = aElts[i];
if (oElement.id)
if (oElement.id == 'alert2')
oElement.onclick = Alert;
}
}

But why this doesn't work:

for (oElement in aElts){
var oElement = aElts[i];
if (oElement.id)
if (oElement.id == 'alert2')
oElement.onclick = Alert;
}
}

Why?
I am attaching two testcases (testcase A works, testcase B doesn't work;
and the JS Console is not warning about anything).

Thanks in advance.

knocte

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
I wrote:
...
But why this doesn't work:

for (oElement in aElts){
var oElement = aElts[i];
if (oElement.id)
if (oElement.id == 'alert2')
oElement.onclick = Alert;
}
}


Sorry, mistake: the correct example is:

for (oElement in aElts){
if (oElement.id)
if (oElement.id == 'alert2')
oElement.onclick = Alert;
}
}

Regards.
Jul 23 '05 #2

P: n/a


knocte wrote:

the correct example is:

for (oElement in aElts){
if (oElement.id)
if (oElement.id == 'alert2')
oElement.onclick = Alert;
}
}


The for .. in loop enumerates the property names of enumerable
properties of an object so for that purpose it is meant and useful (at
least sometimes for user defined objects).
If you have a collection then in JavaScript it usually has a length
property and allows access to elements in the collection with [index]
and/or .item(index) so you need a normal for loop and not a for .. in loop.
There are other languages with for .. in loop constructs which might be
syntactically similar (or even equal) to the one JavaScript has but of
course you shouldn't then assume that what works in one language for you
has to work in JavaScript the same way.

JavaScript is standardized as ECMAScript, the current edition is edition
3. In 2004 an extension to ECMAScript called E4X (ECMAScript for XML)
has been standardized, it adds an additional for each .. in loop
statement to the language which might be closer to what you are looking
for in making the property value available in the loop and not the
property names. However it then applies to all objects and not only
collections and depending on the properties a collection has and whether
they are marked as enumerable even a
for each (var prop in collection) {
// use prop here
}
is not guaranteed to only give you those properties you regard as
elements in the collection. A recent Mozilla 1.8 nightly build when
executing
<script type="text/javascript; e4x=1">
should have support for the for each .. in statement.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3

P: n/a
noticias.mad.ttd.net wrote:
<snip>
I am attaching two testcases ...


Please don't post attachments to comp.lang.javascript. This is a plain
text only group, and many news servers will remove attachments
automatically. See the group's FAQ.

Richard.
Jul 23 '05 #4

P: n/a
knocte wrote:
for (oElement in aElts){
if (oElement.id)
The - for (prop in obj) - construct assigns a sequence of string
property names to the first identified variable (- prop -). When a
string primitive is the left hand operand of the dot operator (in a
property accessor) that string primitive is type-converted into a String
object. String objects do not have - id - properties so the value read
with the property accessor is - Undefined - (the special ECMAScript -
Undefined - value), and - Undefined - type-converts to boolean false for
the - if - statement.
if (oElement.id == 'alert2')
oElement.onclick = Alert;
}
}


So, the execution of the code never enters the outermost - if - block,
but not errors are expected.

Richard.
Jul 23 '05 #5

P: n/a
On Sun, 30 Jan 2005 19:24:03 +0100, Martin Honnen <ma*******@yahoo.de>
wrote:
is not guaranteed to only give you those properties you regard as
elements in the collection. A recent Mozilla 1.8 nightly build when
executing
<script type="text/javascript; e4x=1">


Please don't encourage any further pollution of the text/javascript
mime-type, application/x-javascript;e4x=1 works and makes a lot more
sense.

Jim.
Jul 23 '05 #6

P: n/a
Richard Cornford escribió:
noticias.mad.ttd.net wrote:
<snip>
I am attaching two testcases ...

Please don't post attachments to comp.lang.javascript. This is a plain
text only group, and many news servers will remove attachments
automatically. See the group's FAQ.

Richard.


Sorry for my netiquette lack, but I thought that adding two minimal HTML
attachments (testcases) would not hurt anybody.

Thanks for all replies.

Regards.
Jul 23 '05 #7

P: n/a
Lee
knocte said:

Richard Cornford escribió:
noticias.mad.ttd.net wrote:
<snip>
I am attaching two testcases ...

Please don't post attachments to comp.lang.javascript. This is a plain
text only group, and many news servers will remove attachments
automatically. See the group's FAQ.

Richard.


Sorry for my netiquette lack, but I thought that adding two minimal HTML
attachments (testcases) would not hurt anybody.

Thanks for all replies.


The fact that there were attachments changed your post from plain text
to MIME format. You could have simply included the HTML as text in
the message.

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.