Forum Moderators: open
<head>
<script language=Javascript><!---
function testIt() {
with(document.test_radio) {
if (element[0].checked) {
alert ("You selected yes");
}
else {
alert ("You selected no");
}
}
}
//--></script>
</head>
<body>
<form name="test_radio">
<input type ="radio" name="answer" value=yes><b>Yes</b><br><br>
<input type="radio" name="answer" value=no><b>No</b><br><br>
<input type="button" name="button" value="Submit" onclick="testIt()">
</form>
<form action="http://mydomain.com/cgi-bin/radio_test.cgi"
method="post" name="imlearning">
<input type="hidden" name=info value="">
</form>
I notice you have two forms there. Instead of the with, you can pass the form object to the routine. This would allow you to use the routine from any form and you don't need to reference the form specifically by name:
<head>
<script type="text/javascript">
function testIt(form) {
//for (i=0;i<form.elements.length;i++) {
//alert(form.elements[i].name);
//}
if (form.elements[0].checked) {
alert ("You selected yes");
}
else {
alert ("You selected no");
}
}
</script>
</head>
<body>
<form name="test_radio" onSubmit="return false;">
<input type ="radio" name="answer" value="yes"><b>Yes</b><br><br>
<input type="radio" name="answer" value="no" checked><b>No</b><br><br>
<input type="submit" name="button" value="Submit" onclick="testIt(this.form);">
</form>
A few notes:
- if you quote one, quote them all
- "script language" is deprecated
- use type=submit with return false in form onSubmit so if Javascript is disabled, the form will still submit
- Uncomment my little for... loop to read in all elements in the passed form. Helpful little debugging thingy.