Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I retreive the "value" of a radio button in Javascript?

Status
Not open for further replies.

rickybaz

Programmer
Jan 23, 2001
1
GB
I am having real difficulty retreiving the value of a radio button, I don't even know if this is possible with Javascript. I'm trying to define it as a Javascript variable and the most logical method is not working:
"var option = document.formName.radioButtonName.value"
I would be very grateful if anyone could help! Cheers.
 
Radiobuttons are a pain sometimes - try this.

Matt.

<html>
<head>
<title>Untitled</title>
<script language=&quot;JavaScript&quot;>
<!--

function getSelected(allbuttons){
for (i=0;i<allbuttons.length;i++) {
if (allbuttons.checked) {
return allbuttons.value
}
}
}

function getValue(){
var CheckedButton = getSelected(document.forms[0].myRadioButton)
alert(CheckedButton)
}
//-->
</script>
<form>
<input type=&quot;radio&quot; name=&quot;myRadioButton&quot; value=&quot;one&quot; checked>One
<input type=&quot;radio&quot; name=&quot;myRadioButton&quot; value=&quot;two&quot;>Two
<input type=&quot;radio&quot; name=&quot;myRadioButton&quot; value=&quot;three&quot;>Three
<input type=&quot;button&quot; value=&quot;get value&quot; onClick=&quot;getValue()&quot;>
</form>

</body>
</html>
 
Rickybaz, try the following code, it works:

<script language=&quot;Javascript&quot;>
function getInput(form) {

var option=&quot;&quot;;
for(j=0; j < form.MyRadio.length; j++) {
if(form.MyRadio[j].checked) {
option=form.MyRadio[j].value;
break;
}
}
}
</script>
<form>
<input type=&quot;radio&quot; name=&quot;MyRadio&quot; value=&quot;1&quot;>1<br>
<input type=&quot;radio&quot; name=&quot;MyRadio&quot; value=&quot;2&quot;>2<br>
<input type=&quot;radio&quot; name=&quot;MyRadio&quot; value=&quot;3&quot;>3<br>
<input type=&quot;radio&quot; name=&quot;MyRadio&quot; value=&quot;4&quot;>4<br>
<input type=&quot;radio&quot; name=&quot;MyRadio&quot; value=&quot;5&quot;>5<br><br>
<input type=&quot;button&quot; value=&quot;Klik&quot; onClick=&quot;getInput(this.form)&quot;>
</form>

Hope this helps...

<webguru>iqof188</webguru>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top