Radio buttons

L

Laser Lips

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
 
L

Laser Lips

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
 
E

Evertjan.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top