473,508 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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



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
4 3189
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8594
by: Phil Powell | last post by:
Has anyone here ever done a case where you have a select multiple form element and you have to do both server-side and client-side validation? I am honestly not sure how to do it in Javascript (I...
8
1985
by: adrien | last post by:
Hi, i start with javascript and i saw a lot of examples, but sometimes there is ";" on each end of line, sometimes only certain lines, sometimes nowhere. So my question is: where must i put ";"...
3
3537
by: Albert Wagner | last post by:
I have just had a problem that I cannot find any reference to in any docs. Admittedly, I am a JavaScript newbie, but this sure seems like a bug or an "undocumented feature" in Opera 7.11. I have...
25
3695
by: delerious | last post by:
I see some code examples like this: <DIV onmouseover="this.style.background='blue'"> and other code examples like this: <DIV onmouseover="javascript:this.style.background='blue'"> Which...
7
1431
by: markturansky | last post by:
I'd like to dynamically find and invoke a method in a Python CGI. In javascript, the running script is 'this' (Python's 'self'), except that 'self' is not defined. I want to do this: var m...
11
1723
by: Ux | last post by:
Hi I'm a newbie at JS and I'd like to know, from the expert JS-er, which are the "things" they consider a *must have* (or known/visited/read...) for a professional JS developer. This will help me...
22
2505
by: David. E. Goble | last post by:
Hi All; I have a few of these; sigsImages="sigs/finished1.jpg" sigsImages="sigs/foghorn.jpg" lower I have;
1
1332
by: Peter Michaux | last post by:
Hi, I saw Brendan Eich in an online conference video say that in JavaScript 2 that they will lexically bind the "this" keyword of inner functions. He said that currently the execution-time...
4
3872
by: fran7 | last post by:
Hi, from help in the javascript forum I found the error in some code but need help. This bit of code works perfectly, trouble is I am writing it to a javascript function so the height needs to be in...
7
1305
by: gappodi | last post by:
Why is 0 == "" in JavaScript Since "A" + 0 != "A" + "" thank you. Gap
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.