newman Posted November 28, 2006 Share Posted November 28, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/28730-radio-buttons/ Share on other sites More sharing options...
paul2463 Posted November 28, 2006 Share Posted November 28, 2006 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.HTHPaul Quote Link to comment https://forums.phpfreaks.com/topic/28730-radio-buttons/#findComment-131630 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.