Click to See Complete Forum and Search --> : problems with referencing a radiobutton
damon2003
12-12-2003, 07:26 AM
Hi,
I have ONE radiobutton on my page. I want it to be checked before the user can continue. I currently have this code:
var varradiobutton = document.form1.radiobutton[0];
var noneChosenRadiobutton=true;
if(varradiobutton.checked)
{
noneChosenRadiobutton=false;
}
if (noneChosenRadiobutton)
{
returnVal=false;
alert("You must confirm");
}
it does not work though, it works if I put more than one radiobutton on the page, but not if there is only one button.
How can this be fixed?
thanks
lcscne
12-12-2003, 07:47 AM
radio buttons are meant for multiple mutually exclusive options. change the radio button to a checkbox.
damon2003
12-12-2003, 07:49 AM
I see,
the problem is I have some javscript validation, and it will mess up if I put in anohter checkbox, actually think I will be able to use a yes no option then with radiobuttons
lcscne
12-12-2003, 07:53 AM
as long as you are refferencing the other checkboxes properly in your validation code you should be able to add another checkbox. if you must use a radio button you could add the second one and add the following css to it:
<input type='radio' style='display: none;'>
olerag
12-12-2003, 07:59 AM
You'll need at least two (2) entries for a Radio Group to be
successful. If you only want one, a checkbox can be used.
The example below can be used to demo both...
<html>
<head>
<script type="text/javascript">
function checkRG(form) {
var rgObj = form.radioTest;
if (!rgObj[0].checked) {;
alert("You must confirm.");
}
else {
alert("You may continue.");
}
}
function checkCB(form) {
var cbObj = form.checkTest;
if (!cbObj.checked) {;
alert("You must confirm.");
}
else {
alert("You may continue.");
}
}
</script>
</head>
<body>
<center>
<b>Radio / Checkbox Test</b>
<form>
<input type="radio" name="radioTest" value="Y" checked>Continue
<input type="radio" name="radioTest" value="N" checked>Do Not Continue
<br>
<input type="Button" name="submitRGBtn" value="Radio Test" onClick="checkRG(this.form)">
<hr>
<input type="checkbox" name="checkTest" value="Y" checked>Continue
<br>
<input type="Button" name="submitCBBtn" value="Check Test" onClick="checkCB(this.form)">
</form>
<hr>
</center>
</body>
</html>
olerag
12-12-2003, 08:03 AM
FYI -
"lcscne"'s example of not displaying the second button in
a radio group will work for new browsers. If you believe your
customer(s) may be entering your web pages with, say,
Netscape 4.7x, the "non-displayed" RG button will be
displayed.