SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    I'm in your computer! Halo-13's Avatar
    Join Date
    Jun 2004
    Location
    Minneapolis, MN
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form - check box requirement.

    I have a simple form that contains only a check box and submit button. It's for an agreement on the page I am providing to certain people. How do I get the form to only perform the action if the check box is checked?

    I'm not much of a JavaScript wiz
    Halo-13 Network
    ArcadeApe

  2. #2
    I'm in your computer! Halo-13's Avatar
    Join Date
    Jun 2004
    Location
    Minneapolis, MN
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have found a solution.

    Code:
    <script type="text/javascript">
    <!--//
    
    function freeSubmit(){
    if(document.forms[0][1].disabled==true) {
       document.forms[0][1].disabled=false;
     }
    else {
       document.forms[0][1].disabled=true;
      }
     }
    
    //-->
    </script>
    
    
    <form action="#" >
    <input type="checkbox" onclick="freeSubmit()"><label>I agree to the terms of this site etc.</label>
    <input type="submit" disabled="disabled">
    </form>
    Halo-13 Network
    ArcadeApe

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    So if it's on, you turn it off, and if it's off, you turn it on.

    Why not just use the following?

    Code JavaScript:
    document.forms[0][1].disabled = document.forms[0][1].disabled ? 0 : 1

  4. #4
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yuck, try using id's instead:

    Code:
    <input type="checkbox" id="example" />
    <input type="submit" id="button" value="submit" disabled="disabled" />
    <script>
    var example = document.getElementById('example');
    var button = document.getElementById('button');
    example.onclick = funciton() {
      button.disabled = (!example.checked);
    };
    </script>

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    I agree. I was tossing up whether to suggest using id's but went with what he already had there instead.

    Your solution is a lot easier to understand as well.

    You may wish to edit it to remove the spelling mistake though.

  6. #6
    I'm in your computer! Halo-13's Avatar
    Join Date
    Jun 2004
    Location
    Minneapolis, MN
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Polvero! I like your solution much better, it makes more sense. Function is spelled wrong as pmw57 mentioned above.

    Here is a working code in case someone else wants to use it:
    Code:
    <input type="checkbox" id="example" />
    <input type="submit" id="button" value="submit" disabled="disabled" />
    <script>
    var example = document.getElementById('example');
    var button = document.getElementById('button');
    example.onclick = function() {
      button.disabled = (!example.checked);
    };
    </script>
    Halo-13 Network
    ArcadeApe


Bookmarks

Posting Permissions

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