Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if a radio button is selected

Status
Not open for further replies.

gameon

Programmer
Feb 23, 2001
325
GB
Hi,
I usually use something like:

var selected = document.CalendarFilter.OnDemand;

and check what value 'selected' is to see if something from a dropdown is selected.

How do I check whether a radio button is true or false?

M@?
 
alert(document.FormName.Radiobutton.checked)

Known is handfull, Unknown is worldfull
 
that pops up with undefined:
alert(document.CalendarFilter.OnDemand.checked);

this says true though (but all the time):
document.CalendarFilter.OnDemand[0].value

this says false though (but all the time):
document.CalendarFilter.OnDemand[1].value

??
 
got it: I should have used:

alert(document.CalendarFilter.OnDemand[0].checked);
 
All radio buttons with the same name form an array that is traversable with a for-loop. Try the following:

Code:
<HTML>
<HEAD>
<SCRIPT>
function showVal()
{
 for(i=0; i<RB.length; i++)
  if(RB[i].checked)
   alert(RB[i].value);
}//end showVal()
</SCRIPT>
</HEAD>
<BODY>
<INPUT TYPE=RADIO VALUE=1 NAME=RB> 1<BR>
<INPUT TYPE=RADIO VALUE=2 NAME=RB> 2<BR>
<INPUT TYPE=RADIO VALUE=3 NAME=RB> 3<BR>
<INPUT TYPE=BUTTON VALUE='SHOW VAL' ONCLICK=showVal()>
</BODY>
</HTML>

Good luck!

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top