View Full Version : Checkbox help

06-19-2008, 02:25 PM

I am trying to produce a disclaimer ('Yes, I agree', 'No, I don't') using checkboxes in JavaScript.

If neither box is checked and the user presses 'Submit', they ought to get an alert box informing of the user of this. The folliwng is supposed to do this, but no alert box shows. I have tried looking at the script through IE7 and Firefox 3.

If possible, I would also like the page to immediately redirect to my main page when the 'I agree' check box is selected (but not when the 'I do not agree' checkbox is selected). This removes the need for the 'Submit' button. At the same time, the 'I do not agree' checkbox should be defaulted.

This is what I have so far:


function checkCheckBoxes() {
if (document.frmTest.CHKBOX_1.checked == false &&
document.frmTest.CHKBOX_2.checked == false)
alert ('Please select a checkbox');
return false;
return true;


And then this:

<form onsubmit="return checkCheckBoxes();" action="">
<input type="checkbox" name="CHKBOX_1" value="1">I do not agree to the terms</p>
<input type="checkbox" name="CHKBOX_2" value="2">I agree to the terms</p>
<input type="submit" value="Submit!" />

Thanks for any help with my awkward questions!


Philip M
06-19-2008, 02:37 PM
As it stands you problem, is:-

<form name = "frmTest" onsubmit="return checkCheckBoxes();" action=""

But if you want to redirect when "I agree" is clicked:-

<script type="text/javascript">
function redirect() {
window.location = mainpage.html; // of course you must specify the correct name of your page

<form name = "myform">
<input type="checkbox" name="CHKBOX_1" value="1" checked>I do not agree to the terms</p>
<input type="checkbox" name="CHKBOX_2" value="2" onclick = "redirect()">I agree to the terms</p>

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

06-19-2008, 03:10 PM
Hello Phillip

Many thanks for your reply. You make it look so easy!

The page loaded with 'I do not agree' already checked as I wish, but it didn't redirect - a little yellow exclamation mark appeared in the bottom left-hand corner of the screen.

So I replaced myfile.html with 'myfile.html' and it is OK now.

Very many thanks.