473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

radiobuttons and radio groups

Hi,

Can someone explain why this works:

for (i=0; i<document.form1.radiogroup.length; i++) {
var myRadio = document.form1.radiogroup[i];
}

but this does not:

for (i in document.form1.radiogroup) {
var myRadio = document.form1.radiogroup[i];
}

I'm sure this is down to a simple lack of understanding on my part. I have
hunted round all the w3c recommendations for DOM and HTML and the ECMAScript
bindings, but I can't find anything that properly explains the programming
model for radio groups. Have I missed something?

Andy
Jul 20 '05 #1
5 2802

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:oe**********@hotpop.com...
"Andy Fish" <aj****@blueyonder.co.uk> writes:
Can someone explain why this works:

for (i=0; i<document.form1.radiogroup.length; i++) {
var myRadio = document.form1.radiogroup[i];
}

but this does not:

for (i in document.form1.radiogroup) {
var myRadio = document.form1.radiogroup[i];
}
How does it not work? Does it give a runtime error and stop, or does it
just not give the result you expect?


I was deliberately not giving all the detail because I think it's clear from
the context what I'm trying to do. What I want to do is iterate through all
the radio buttons in the group.
I'm sure this is down to a simple lack of understanding on my part. I have hunted round all the w3c recommendations for DOM and HTML and the ECMAScript bindings, but I can't find anything that properly explains the programming model for radio groups. Have I missed something?
A radiogroup is not an array. When you use for(..in..), you iterate
through all the visible properties of the object. In a radiogroup
object, there are more visible properties than just the integer named
ones. One of them is the "namedItem" method. When I run the second
example, the one you say doesn't work, the final value of myRadio
is the "namedItem" function.


I'm happy to accept that it's not an array. I understand that it could be an
object with a property called length. However, in that case I can't see how
the square bracket notation in the second example works. I thought that
square brackets applied to an object returned the named properties.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #2
"Andy Fish" <aj****@blueyonder.co.uk> writes:
I was deliberately not giving all the detail because I think it's
clear from the context what I'm trying to do. What I want to do is
iterate through all the radio buttons in the group.
I did assume that some code was missing that would operate on the
myRadio variable. The code you presented would run with no *errors*. I
asked for how the examples failed, mostly to check what you did
expect.
I'm happy to accept that it's not an array. I understand that it
could be an object with a property called length. However, in that
case I can't see how the square bracket notation in the second
example works. I thought that square brackets applied to an object
returned the named properties.


The square bracket notation returns the value of a given property.

If "namedItem" is a property of the object "o", then "o['namedItem']"
will return the value of that property.
If you write
for (var i in o) {
... o[i] ...
}
then you will do something with the values of each property of "o".

The "for(i in obj)" construction iterates throught the names of the
enumerable properties of "obj". All the values that "i" assume are
properties of the object, but some properties are excluded (the
non-enumerable ones).

The enumerable properties of the NodeList (the radio group collection)
differs between browsers.

If I make a form (called "foo") with four radiobuttons in a group
called "bar", and use the following code to show the properties of it:
var x="";for(var i in document.forms.foo.bar) {x+=i+"\n"};alert(x)
then I get the following results.

In Mozilla, I can see the properties: item, length.

In Opera 7, I can see the properties: tags, item, namedItem.

In IE 6, I can see the properties: length, bar, bar, bar, bar. (That
is REALLY spurious, since it claims to have the same property four
times. That is, the for(..in..) construct doesn't work as it should).
In your case, you want to iterate through *some* of the properties of
the collection of radio buttons (the ones that are integers, which are
not enumerable in any browser). For that you cannot use
"for(...in...)", since it iterates through *all* the *enumerable*
properties, but you must count from 0 to length-1 manually.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:he**********@hotpop.com...
<snip>
... . That is, the for(..in..) construct doesn't work as it should).

<snip>

I would have thought that DOM elements, collections, nodeLists and such
like would be classified as "Host Objects" (ECMA 262 section 4.3.8), in
which case I don't think that there is any defined behaviour for the -
for(var prop in obj) - construct.

Without specified behaviour for - for(var prop in obj) - when applied to
a Host Object any results achieved will be coincidence and it would be
unrealistic to expect them to be consistent across browsers. Try your
test on Opera 6, I doubt that you will even see 'length' enumerated.

Richard.
Jul 20 '05 #4
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:d6**********@hotpop.com...
<snip>
... . I still find IE's choice of implementation dependent
behavior highly curious and counterintuitive, when it
allows the for(..in..) construction to give the same value
more than once.

<snip>

Yes it is odd, and unhelpful as I would still expect each property
access by name to return the same object member (probably the first
(whatever that means)). I suspect that this provides additional evidence
for your suggestion of a couple of weeks ago that IE implements its
objects as some sort of list instead of as a HashTable, as a HashTable
could only have one property of the same name.

(You have, of course, realised why I likened trying to help George
Hester to beating your head against a wall (the best part of which is
when you stop) ;-)

Richard.
Jul 20 '05 #5
Thanks for all this comment folks, I certainly have a better understanding
of what's going on now.

to return to one of the questions in my original post, can anybody point me
to a specification that describes the ECMAScript programming model for HTML
form controls - it seems to fall in a gap between the specs for HTML, DOM,
and the language itself.
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:bh*******************@news.demon.co.uk...
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:d6**********@hotpop.com...
<snip>
... . I still find IE's choice of implementation dependent
behavior highly curious and counterintuitive, when it
allows the for(..in..) construction to give the same value
more than once.

<snip>

Yes it is odd, and unhelpful as I would still expect each property
access by name to return the same object member (probably the first
(whatever that means)). I suspect that this provides additional evidence
for your suggestion of a couple of weeks ago that IE implements its
objects as some sort of list instead of as a HashTable, as a HashTable
could only have one property of the same name.

(You have, of course, realised why I likened trying to help George
Hester to beating your head against a wall (the best part of which is
when you stop) ;-)

Richard.

Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: discomiller | last post by:
Mario Mueller: Hello *, radiobuttons belong to other radiobuttons by the "name="any_value"" attribut. Thats a fakt. I got the following XML:...
3
by: Martien van Wanrooij | last post by:
I am working on a site with some pages that all have a form that starts with a group of radiobuttons. By default none of the buttons is checked. Before submitting the form there is a validation...
0
by: Carlos | last post by:
Hi again all, I am now trying to process two groups of radiobuttons, and determine answers for a survey. While handling the submit button, I would like to know ewhat is the best way of checking...
11
by: Rourke Eleven | last post by:
I have looked and searched. What good is the databind property on Radiobuttons? How does one go about actually using it? What is a good resource on this? I understand that I can easily get/set...
2
by: Chris Ashley | last post by:
I have a couple of RadioButtons both with the same GroupName. Is there any way I can use a RequiredFieldValidator to ensure that at least one of the RadioButtons in the group is selected? Setting...
4
by: news.microsoft.com | last post by:
Hello, I have two databound radiobuttons which have advanced databinding properties of onpropertychanged enabled so that I can raise the columnchanged event whenever the radiobuttons are...
0
by: Jim in Arizona | last post by:
In html and ASP, I would easily code radio buttons as needed, such as: <input type="radio" name="question1" value="1">1</input>&nbsp; <input type="radio" name="question1"...
10
by: Carlos | last post by:
Hi all, I have a form with an input radio control in a template field. When the user selects an option, and press a button the selection disappears.. I would like the selection to persist after...
4
by: TechnoAtif | last post by:
Hi all. I have stuck up with a new problem.Hope anyone can find some solution for that. I have three radiobuttons and the situation is that on click of each radio button a new php page is to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.