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

Radio buttons

Hi All. This one has plagued me for a while.

I want to get the value of the selected radio button in 2 ways.
Bearing in mind that the radio buttons are always in a form tag.

1) I want to be able to get the value by using somthing like the
following...

myRadio = document.getElementById("myRadio");
//then somehow get the value of the selected radio button.

2) Have a function which is pased the form element and get to it that
way. eg.

function getVals(form)
{
myrad = form.myRadio
//then somehow get the value of the selected radio button.
}

=========================

Playing with other code i was able to get the value by doing the
following...

<HTML>
<HEAD>
<script>
function GO()
{
alert(rad.length);
for(x=0;x<rad.length;x++)
{
if(rad[x].checked==true)
{
alert(rad[x].value);
}
}
}
</script>
</HEAD>
<BODY>
<input type='radio' name='rad' id='rad' value='answer1'/>1
<input type='radio' name='rad' id='rad' value='answer2'/>2
<input type='radio' name='rad' id='rad' value='answer3'/>3
<input type='radio' name='rad' id='rad' value='answer4'/>4
<button onclick='GO();'>GO</button>
</BODY>
</HTML>

=========================

BUT I dont want to be able to just access the radio button direclty
because in some applications of this code, it doesnt work.

I really want to use document.getElementById and then continue from
that.

Any suggestions?
Graham
Aug 22 '08 #1
2 1270
On Aug 22, 10:21 am, Laser Lips <loudsphi...@gmail.comwrote:
Hi All. This one has plagued me for a while.

I want to get the value of the selected radio button in 2 ways.
Bearing in mind that the radio buttons are always in a form tag.

1) I want to be able to get the value by using somthing like the
following...

myRadio = document.getElementById("myRadio");
//then somehow get the value of the selected radio button.

2) Have a function which is pased the form element and get to it that
way. eg.

function getVals(form)
{
myrad = form.myRadio
//then somehow get the value of the selected radio button.

}

=========================

Playing with other code i was able to get the value by doing the
following...

<HTML>
<HEAD>
<script>
function GO()
{
alert(rad.length);
for(x=0;x<rad.length;x++)
{
if(rad[x].checked==true)
{
alert(rad[x].value);
}
}
}
</script>
</HEAD>
<BODY>
<input type='radio' name='rad' id='rad' value='answer1'/>1
<input type='radio' name='rad' id='rad' value='answer2'/>2
<input type='radio' name='rad' id='rad' value='answer3'/>3
<input type='radio' name='rad' id='rad' value='answer4'/>4
<button onclick='GO();'>GO</button>
</BODY>
</HTML>

=========================

BUT I dont want to be able to just access the radio button direclty
because in some applications of this code, it doesnt work.

I really want to use document.getElementById and then continue from
that.

Any suggestions?
Graham
Never mind, if I get the form by doing a document.getElementById on
the form and then using the . operator like this, it works....

theForm = document.getElementById("myForm")
alert(theForm.myRadio.length);
Graham
Aug 22 '08 #2
Laser Lips wrote on 22 aug 2008 in comp.lang.javascript:
<input type='radio' name='rad' id='rad' value='answer1'/>1
<input type='radio' name='rad' id='rad' value='answer2'/>2
<input type='radio' name='rad' id='rad' value='answer3'/>3
<input type='radio' name='rad' id='rad' value='answer4'/>4
Illegal.

id's must be unique.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 22 '08 #3

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

Similar topics

1
by: sman | last post by:
Hi, I recently read this article on About.com on how to create required fields for a form: http://javascript.about.com/library/scripts/blformvalidate.htm Everything works great except that there...
2
by: Jeff | last post by:
I'm trying to create a dynamic form that can have multiple groups of radio buttons (each group has two buttons) with the same name. Essentially, the form allows a user to enter as many names as...
4
by: Oscar Monteiro | last post by:
I Have to sets of Radio buttons like so: <input type="radio" name=p1 value=1> <input type="radio" name=p1 value=2> <input type="radio" name=p1 value=3> <br> <input type="radio" name=p2 value=1>...
6
by: Craig Keightley | last post by:
I have a page that has n number of radio groups (yes/No) how can i prevent the form being submitted if more than one radio group is not selected? By default all radio groups are unchecked ...
2
by: Rob | last post by:
Hi all, I've got multiple sets of radio button that are dynamically created in code and populated by a database query. The query returns about 20 recordsets with 3 radio buttons per recordset and...
2
by: James P. | last post by:
Help, I need to display radio buttons on a form. The data is from SQL table: each row in each table is displayed as a radio button. I have multiple SQL tables so I understand I need to put...
1
by: kenny8787 | last post by:
Hi, can anyone help here? I have the following code generated from a database, I want to have javascript calculate the costs of the selected items using radio buttons, subtotal the costs and...
2
by: dpazza | last post by:
Hi, I'm creating a quiz on using a form in VB 2005 express. I have four sets of questions and answers (labels and radio buttons) and I change between which set of questions is currently shown on...
4
by: Blasting Cap | last post by:
I have a page that has a number of radio buttons that will be displayed to different access levels of a user who logs in to my website. For instance, if there are a dozen buttons, user1 will see...
11
by: Twayne | last post by:
Hi, Newbie to PHP here, no C or other relevant background, so pretty niave w/r to the nuances etc. but I think this is pretty basic. XP Pro, SP2+, PHP 4.4.7, XAMPP Local Apache Server...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.