Solved

check if radio is checked

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query not working correctly? (or how i want it to... 6 50
Question about ASP Including Files 6 59
ASP Button to clear text 4 54
Questions about INCLUDE FILES 2 31
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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
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…

773 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