[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

check if radio is checked

Posted on 2004-08-30
5
Medium Priority
?
347 Views
Last Modified: 2011-04-14
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
0
Comment
Question by:campanal
5 Comments
 
LVL 15

Expert Comment

by:justinbillig
ID: 11930039
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 );">
0
 
LVL 33

Accepted Solution

by:
sajuks earned 160 total points
ID: 11930060
<script language="JavaScript">
<!--
function radio_button_checker()
{
var radio_choice = false;
for (counter = 0; counter < radio_form.radio_button.length; counter++)
{
if (radio_form.radio_button[counter].checked)
radio_choice = true;
}
if (!radio_choice)
{
alert("Please select a letter.")
return (false);
}
return (true);
}

-->
</script>


<form method="get" action="http://www.google.com"
onsubmit="return radio_button_checker()" name="radio_form">
<input type="radio" value="A" name="radio_button">A
<br>
<input type="radio" value="B" name="radio_button">B
<br>
<input type="radio" value="C" name="radio_button">C
<br>
<input type="radio" value="D" name="radio_button">D
<br>
<input type="submit" value="Submit">
</form>

</body>
</html>
0
 
LVL 1

Expert Comment

by:Webby_
ID: 11930134
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..
0
 

Author Comment

by:campanal
ID: 11931082
great  sajuks you are the best!!
0
 
LVL 33

Expert Comment

by:sajuks
ID: 11938426
Glad to help you out. Thanks for the points and grade.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question