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

Is there a "for...in" in javascript?

P: n/a


All,

I've been unable to find out if javascript supports
for (var e in obj)
type of looping syntax.
Does it? If so, is this for DOM browsers only?

TIA!

--
--
~kaeli~
If that phone was up your a$$, maybe you could drive a
little better!
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
kaeli <ti******@NOSPAM.comcast.net> writes:
I've been unable to find out if javascript supports
for (var e in obj)
type of looping syntax.
Where have you tried looking?
Why did you think of the syntax to begin with? :)
Does it?
Yes.

<URL:http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/stmt.html#1004815>
<URL:http://msdn.microsoft.com/library/en-us/script56/html/js56jsstmforin.asp>

Also check ECMA 262 v3, section 12.6.4.
If so, is this for DOM browsers only?


What is a DOM browser? One that supports the W3C DOM {1,2}
specification, or just any DOM?

Probably not, though. It existed in Netscape 2 (i.e., JavaScript 1.0),
the very first browser with Javascript.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
In article <u1**********@hotpop.com>, lr*@hotpop.com enlightened us
with...
kaeli <ti******@NOSPAM.comcast.net> writes:
I've been unable to find out if javascript supports
for (var e in obj)
type of looping syntax.
Where have you tried looking?


Google. :)
The search for such common words as for and in was bringing back a TON
of matches. heh
Why did you think of the syntax to begin with? :)
Java has it.
Does it?


Yes.

<URL:http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/stmt.html#1004815>
<URL:http://msdn.microsoft.com/library/en-us/script56/html/js56jsstmforin.asp>


Hey, cool!

Also check ECMA 262 v3, section 12.6.4.
If so, is this for DOM browsers only?


What is a DOM browser? One that supports the W3C DOM {1,2}
specification, or just any DOM?


W3C DOM level 1 was what I had in mind.
Looks like it does. This actually surprises me because I never see it
used (by never, I mean in examples on the web, tutorials, etc) in what I
would think would be a common way - looping through form elements for
validation. I always see stuff like
for (x=0; x<formname.elements.length-1; x++)

Seems to me this would be a lot nicer with
for (var e in document.forms["formname"].elements)

Even better, what I wanted it for was looping through a select element
that was dynamically generated (thus, I don't know indexes, only some
possible values).

Simple example:
var mySelect = document.forms["myForm"].elements["mySelect"];
for (var o in mySelect.options)
{
if (someBoolean) o.selected = true;
alert(o.value+" selected!");
}
--
--
~kaeli~
Who is General Failure and why is he reading my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #3

P: n/a
kaeli <ti******@NOSPAM.comcast.net> writes:
Looks like it does. This actually surprises me because I never see it
used (by never, I mean in examples on the web, tutorials, etc) in what I
would think would be a common way - looping through form elements for
validation. I always see stuff like
for (x=0; x<formname.elements.length-1; x++)

Seems to me this would be a lot nicer with
for (var e in document.forms["formname"].elements)
The problem is that you don't know what will be included.
Object properties have a hidden property that says whether they
are enumerable or not. All the properties of Array.prototype, as well
as array lengths, are not enumerable, so doing
for ( var i in arrayRef ) {...}
works. However, in some browsers, the form's elements' "item" property
is enumerable, as are both the named and numbered properties, so
using your code above on the form
<form id="formname" action="">
<input type="text" name="a">
<input type="radio" name="b" value="x1">
<input type="radio" name="b" value="x2">
</form>
will give some of the following properties:
"a", "b", "0", "1", and "2", and a lot more.
Probably not what you had in mind :).

Hmm, let's try:
Opera 7 gives: item, tags, namedItem
Mozilla gives: length, item, namedItem
Netscape 4 gives: a, 0, 1, b, 2, length, name, elements, method,
action, encoding, target
IE 6 gives (brace yourself): language, scrollHeight, isTextEdit,
currentStyle, document, onmouseup, oncontextmenu, isMultiLine,
clientHeight, onrowexit, onbeforepaste, onactivate, scrollLeft,
lang, onmousemove, onmove, onselectstart, parentTextEdit,
oncontrolselect, canHaveHTML, onkeypress, oncut, onrowenter,
onmousedown, onpaste, className, id, onreadystatechange,
onbeforedeactivate, hideFocus, dir, isContentEditable, onkeydown,
clientWidth, onlosecapture, parentElement, ondrag, ondragstart,
oncellchange, recordNumber, onfilterchange, onrowsinserted,
ondatasetcomplete, onmousewheel, ondragenter, onblur, onresizeend,
onerrorupdate, onbeforecopy, ondblclick, scopeName, onkeyup,
onresizestart, onmouseover, onmouseleave, outerText, innerText,
onmoveend, tagName, title, offsetWidth, onresize, contentEditable,
runtimeStyle, filters, ondrop, onpage, onrowsdelete, tagUrn,
offsetLeft, clientTop, style, onfocusout, clientLeft,
ondatasetchanged, canHaveChildren, ondeactivate, isDisabled,
onpropertychange, ondragover, onhelp, ondragend, onbeforeeditfocus,
disabled, onfocus, behaviorUrns, accessKey, onscroll,
onbeforeactivate, onbeforecut, readyState, all, sourceIndex,
onclick, scrollTop, oncopy, onfocusin, tabIndex, onbeforeupdate,
outerHTML, innerHTML, ondataavailable, offsetHeight, onmovestart,
onmouseout, scrollWidth, offsetTop, onmouseenter, onlayoutcomplete,
offsetParent, onafterupdate, ondragleave, children, parentNode,
nodeValue, name, length, onreset, onsubmit, lastChild, elements,
attributes, acceptCharset, action, method, nodeType, target,
previousSibling, ownerDocument, nodeName, childNodes, nextSibling,
firstChild, encoding, a, b, b
(yes, b is there twice!)

IE's result can be explained by it's desing: The form.elements
reference points to the form element itself.

It works wonders for objects you have made yourself, like ones
you use as hash tables.
for (var o in mySelect.options)


Same problem. DOM nodes have no standard saying which properties are
enumerable, and it differes between browsers.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a
In article <ll**********@hotpop.com>, lr*@hotpop.com enlightened us
with...

Seems to me this would be a lot nicer with
for (var e in document.forms["formname"].elements)
The problem is that you don't know what will be included.


I'm finding that out. This explains the problem in my other post,
especially what happened with IE.
Object properties have a hidden property that says whether they
are enumerable or not. All the properties of Array.prototype, as well
as array lengths, are not enumerable, so doing
for ( var i in arrayRef ) {...}
works. However, in some browsers, the form's elements' "item" property
is enumerable, as are both the named and numbered properties, so
using your code above on the form
<form id="formname" action="">
<input type="text" name="a">
<input type="radio" name="b" value="x1">
<input type="radio" name="b" value="x2">
</form>
will give some of the following properties:
"a", "b", "0", "1", and "2", and a lot more.
Probably not what you had in mind :).

No, not at all.
In Java, if I have an object with a property that is an array of objects
(which is what I was thinking a form was), I get the kind of thing I was
expecting.
I wasn't expecting the garbage I got from trying this with a select
element. heh
Hmm, let's try: <snip>

Damn!

Well, there goes that idea.
*sigh*

IE's result can be explained by it's desing: The form.elements
reference points to the form element itself.

It works wonders for objects you have made yourself, like ones
you use as hash tables.


And that's what the tutorial showed it being used on.
I rarely uses hashes in JS. I use them in Java, which is where I got the
notion.
Oh well.

Thanks!!

--
--
~kaeli~
Synonym: the word you use in place of a word you can't
spell.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.