Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Checkbox help

  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post

    Checkbox help

    Hello

    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:

    <SCRIPT TYPE="text/javascript" LANGUAGE=JAVASCRIPT>

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

    </SCRIPT>


    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!" />
    </form>

    Thanks for any help with my awkward questions!

    Steve

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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
    }
    </script>

    <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>
    </form>


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

  • Users who have thanked Philip M for this post:

    SteveH (06-19-2008)

  • #3
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post
    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.

    Steve


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •