JavaScript - Validating Radio Buttons

Soldato
Joined
26 Aug 2005
Posts
6,901
Location
London
Hi guys,

I am firstly trying to alert the user if a particular radio button has been clicked, but I cannot get the function to work.

Code:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript">

function checkForm()
{
alert("testing form");
	
	if(document.form[0].table[0].cola.checked)
		alert("test");
}

</script>


</head>


<body>

<form ID="myForm" onsubmit = "return checkForm()">

        <table ID="mytable" border="1">
         <th> Machine
         </th>
            <tr>
                    <td> Mike Shake <input type="Radio" name="milkshake"  /> Cola <input type ="Radio" name="cola"  /> 			                         Orange <input type ="Radio" name="orange" />
                    </td>
             </tr>
            <tr>
            	<td> <input type="submit" value="Sumbit" /></td>
            </tr>
        
        
        </table>

</form>


</body>
</html>
 
Code:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript">

function checkForm()
{
alert("testing form");
	
	if(document.getElementById("cola").checked)
		alert("test");
}

</script>


</head>


<body>

<form ID="myForm" onsubmit = "return checkForm()">

        <table ID="mytable" border="1">
         <th> Machine
         </th>
            <tr>
                    <td> Mike Shake <input type="Radio" name="milkshake"  /> Cola <input type ="Radio" id="cola" name="cola"  /> 			                         Orange <input type ="Radio" name="orange" />
                    </td>
             </tr>
            <tr>
            	<td> <input type="submit" value="Sumbit" /></td>
            </tr>
        
        
        </table>

</form>


</body>
</html>

2 things changed - the function and i needed to add an "id" to the cola value on the form. :)
 
Back
Top Bottom