Jump to content

radio buttons


newman

Recommended Posts

Hi all .
i have a form en i wanna pass its values to a JS function .reading a text element value is easy but i can not read the radio button value. how can i do that ?

[code]
<form name="MyForm" method="GET">
<input type="text" name="fname">
<input type="radio" name="gender" value="m">Male
<input type="radio" name="gender" value="f">Female

<input type="button" value="SendToFunction" onclick="MyFunction(this.form.fname.value,[ How can read gender value ??? ])">

</form>
[/code]
Link to comment
Share on other sites

the number of radio buttons on a form, with the same name can be counted and checked to see if they are selected or not, starting from 0 to however many you have. the easiest way I have found to do this type of thing is to write a quick function to check which one is selected, you could extend it to check to see that one of them has actually been selected, but I dont want to do all the work for you.
simple function code
[code]
function genderCheck()
{
if (MyForm.gender[0].checked]){
var returnval = "Male";
}
else
{
var returnval = "Female"
}
return returnval
}
[/code]
the in the onsubmit part you could put something like this
[code]
onsubmit="var gender = genderCheck()"
[/code]
then pass the variable in your onclick function.
HTH
Paul
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.