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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing radio button state

Status
Not open for further replies.

tabbytab

Technical User
Mar 21, 2005
74
GB
I am trying to test the state of individual radio buttons.. without success. What am I missing

Extract of my code
Code:
<SCRIPT LANGUAGE="JavaScript">
var counter=0;
function J ()
{ 

   IF(document.forms[0].A.checked)
   {
   alert "Radio button A is checked";
   }
   
   IF(document.forms[0].A.checked)
   {
   alert "Radio button B is checked";
   }
	counter++;
		document.getElementById('textfield').value=counter;
	
</SCRIPT>

<body>
<Form Name="example">
<input name="A" type="radio" value=1  onClick="J();" />
<input name="B" type="radio" value=2  onClick="J();"/>
<input type="text" name="textfield">
</Form>
</body>


Many thanks in advance
TabbyTab :)
 
Code:
<SCRIPT LANGUAGE="JavaScript">
var counter=0;
function J()
{ 
   if (document.forms[0].A.checked)
   {
   alert[red]([/red]"Radio button A is checked"[red])[/red];
   }
   
   if (document.forms[0].B.checked)
   {
   alert[red]([/red]"Radio button B is checked"[red])[/red];
   }
    counter++;
        document.getElementById('textfield').value=counter;
}    
</SCRIPT>
<body>
<Form Name="example">
<input name="A" type="radio" value=1  onClick="J();" />
<input name="B" type="radio" value=2  onClick="J();"/>
<input type="text" name="textfield">
</Form>
</body>
 
OK... too many errors to list here... all of the points below are relevant to the code you posted:

- watch your use of capitals (javascript is case sensitive)
- use indenting to help catch non-closed brackets etc
- make sure you use the correct <script> definition
- radios share the same name to act like radios

Here is my corrected variation of code... working fine in Firefox on Windows:
Code:
<script type="text/javascript">
var counter=0;
function J() {
	if (document.forms['example'].A[0].checked) {
		alert("Radio button A is checked");
	}

	if (document.forms['example'].A[1].checked) {
		alert("Radio button B is checked");
	}
	counter++;
	document.getElementById('textfield').value=counter;
}
</script>
...
<body>
<form name="example">
	<fieldset>
		<input name="A" id="A" type="radio" value="1" onClick="J();" />
		<input name="A" id="B" type="radio" value="2" onClick="J();"/>
		<input type="text" id="textfield" />
	</fieldset>
</form>
</body>
Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Giving your forms, form elements, functions, and variables meaningful names is also a very good idea. Not only does it help you to remember what is what, it helps others who are trying to help you debug your code.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top