Link to home
Start Free TrialLog in
Avatar of campanal
campanal

asked on

check if radio is checked

hi experts,

i have a question for you:

how can i check if one of the radio button that I have in the page is checked?

sth like:

if (radio is cheked) return true
else alert("no radio checked" return false
Avatar of justinbillig
justinbillig

well this is more of a javascript question ... but theres a couple way's to do it ...

but if you make a function you could do this

function IsRadioButtonChecked( )
{
           alert( document.formname.radiobuttoname.checked );

            return document.formname.radiobuttonname.checked;
}


or you could pass the function the radio button you want to use

function IsRadioButtonChecked( objRadioButton )
{
           alert( objRadioButton.checked )
           return objRadioButton.checked;
}


so you could do this


<input type="button" name="btnTest" value="Click Me To See If A Radio Button Is Checked" onclick="IsRadioButtonChecked( );">


or

<input type="button" name="btnTest" value="Click Me To See If A Radio Button Is Checked" onclick="IsRadioButtonChecked( document.formname.radiobuttonname );">
ASKER CERTIFIED SOLUTION
Avatar of sajuks
sajuks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And if you do want to do it in an asp page after submit then it returns it's value if checked, check it like this:

If Request.Form("radiobuttonName") <> "" Then
  'True
Else
  'False
End If

Be sure the name and value are set or else it still won't work.
You can also have more radiobuttons with the same name, the checked one gives his value then. For example both names are name="gender" and the value of each is: value="Male" and value="Female"
Then you can see with Request.Form("gender") what value is chosen..
Avatar of campanal

ASKER

great  sajuks you are the best!!
Glad to help you out. Thanks for the points and grade.