473,395 Members | 1,688 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,395 software developers and data experts.

accessing radiobuttons

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 script that verifies if a choice
has been made.

The following code works, but only for one individual page
function showChecked()
{
if(!document.forms[0].heeftPartner[0].checked &&
!document.forms[0].heeftPartner[1].checked)alert ('Please answer this
question');
else document.forms[0].submit();
}

In every page there will come an almost similar script, only the number of
radiobuttons belonging to the same group varies and every group also has a
different name.
As far as I tried out, when I write document.forms[0].elements[0] the
"heeftPartner" group can also be accessed, but I didn't find a method to
acces every individual element. So I would like a script that
- chooses the first element of the first form (which is always a radio
button group)
- verifies if one of the radio buttons has been checked
If I could retrieve the number of buttons in a group by something like
forms[0].elements[0].length I could make something like

var theLength = forms[0].elements[0].length
var isAnswered = false;
for(i = 0; i < theLength; i++)
{
if (document.forms[0].element[0][i].checked) //and obviously this does
not work!!!
{
isAnswered = true;
break;
}
}
if (isAnswered) document[0].forms[0].submit();
else alert('Please answer this question');

Even if the script has to be a little bit more complicated it is still worth
the trouble because there are a lot of pages.
Thanks for any suggestions,

Martien van Wanrooij
Jul 20 '05 #1
3 1619
On Sun, 18 Jan 2004 11:56:49 +0100, Martien van Wanrooij
<in**@martienvanwanrooij.nl> wrote:
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 script that
verifies if a choice has been made.


When you use form.elements[index], where form represents a form object and
index is a positive integer, you will obtain a reference to the control at
that index in the form. If you use form.elements['name'], where name is
the name of the control, you will obtain a collection of all controls in
that form, with that name.

That's how you can access a group of radio buttons[1].

That should help you on your way,
Mike
[1] There are other methods.

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2

"Michael Winter" <M.******@blueyonder.co.invalid> schreef in bericht
news:op**************@news-text.blueyonder.co.uk...
If you use form.elements['name'], where name is
the name of the control, you will obtain a collection of all controls in
that form, with that name.

Thank you, Michael, this works exactly as I wanted :)

Martien van Wanrooij
Jul 20 '05 #3
Here's how to reference using DHTML (vice DOM)...

A set of radio buttons that work together all have the same name - so if
you reference that name you get an array of all radio buttons that have
that same name. So here is some shell code that iterates through all the
radio buttons...

<input type'radio' name='heeftPartner'
onclick='doSomething(document.formname.heeftPartne r);'
value='whatever'>whatever</input>
<!-- more radio buttons here all w/ the same name -->

<script>
function doSomething (buttonList) {
for (var i=0; i=buttonList.length; i++) {
if (buttonList[i].selected == true) {
// do something if this is selected
}
}
}
</script>

Martien van Wanrooij wrote:
"Michael Winter" <M.******@blueyonder.co.invalid> schreef in bericht
news:op**************@news-text.blueyonder.co.uk...
If you use form.elements['name'], where name is
the name of the control, you will obtain a collection of all controls in
that form, with that name.


Thank you, Michael, this works exactly as I wanted :)

Martien van Wanrooij


Jul 20 '05 #4

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:...
1
by: --=|3s|=-- | last post by:
Hi, I'm trying to create a form in VISUAL BASIC .NET wich shows values of records from a customers table. For example a form wich shows the name, surname and sex of a person. For the name &...
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: PaulS | last post by:
Hello! I put on Form PictureBox and I set big image. Next I put many radiobuttons (80) on PictureBox, but I have problem. I want that 10 radiobuttons have own Checked, next part of 10...
4
by: PedroVision | last post by:
I have a VB 2005 form with 140 different radiobuttons... Some are "checked", some are not... I want to reset all radiobuttons to "unchecked". I've been searching and trying differnet...
8
by: =?Utf-8?B?UmljaA==?= | last post by:
If you enclose a group of radiobuttons (option buttons in MS Access) in an option group control (a frame control) in Access -- the frame control will return the index of the option button that is...
0
by: | last post by:
I am using radiobuttons in a gridview, and not a column of radiobuttons. I have one cell in each row with several radiobuttons that the user has to choose a radiobutton within each row. How can I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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...

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.