Solved

check if radio is checked

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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