Solved

check if radio is checked

Posted on 2004-08-30
5
302 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 40 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual Project Bridge error 5 56
Classic ASP - SQL Server connection problem 5 51
Need help editing script 3 54
ASP Focus problem 3 30
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 information …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now