Javascript is saying that Radio button selection not made even though it is made!


Why is the input type = radio on this page is not getting the value assigned to it after the user clicks on the related selection?

Here is the page under development: - TOTALLY FREE online dating service - date singles ready for romance and love

You can see this by selecting a choice for "I am a: " Radio button selection. I have set up Javascript code, alert, which will tell you that that selection is “Undefined” even though you have selected male or female.


You are testing data.mygender.value which is undefined as data.mygender is an array and not a single entry.

You should be testing data.mygender[data.myGender.selectedIndex].value in order to read the value of the entry in the array that is currently selected.


But mygender is an input of Radio type and NOT checkbox type, so it is not in an array.

I mean to read its value via Php for example one would do:

$mygender = $_POST[‘mygender’];

I mean:
1- I have never seen this in case of Radio value retrieval

2- what would selectedIndex be?

Your statement is correct for server side languages since only the selected entry gets passed to the server from radio buttons.

JavaScript has to deal with all of what is in the web page though and so for JavaScript radio buttons are the ONLY form field that is ALWAYS an array because there is always more than one of them with the same name. Chechkboxes are not usually arrays since there is usually only one checkbox for each name.

JavaScript determines which entry in a radio button array by using the selectedIndex attribute as the number representing the entry in the radio button array that is selected.

In JavaScript radio buttons are an array because JavaScript sees all the buttons whether selected or not. To server side languages radio buttons are not an array because only the selected button is passed. You can’t base how JavaScript works on server side languages because JavaScript sees the entire web page content whereas server side languages only see the values submitted back to the server.

I see.
Ok, thanks for clarifying that.
I tell you as a developer one learns a few new things per day :slight_smile: