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 6 of 6
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Submit button enable

    Hi, I was just wondering how to make a submit button enabled when a checkbox is clicked (for a terms and conditions kinda thing).

    Thanks

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Try this:
    Code:
    <html>
    <head>
    </head>
    <body>
    <form name="myForm" acton="">
    Terms:<input type="checkbox" name="chk" onclick="if(this.checked) document.myForm.sub.disabled=false; else document.myForm.sub.disabled=true;"><br>
    <input type="submit" name="sub" disabled="true">
    </form>
    </body>
    </html>

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    thanks

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    You can simplify the code by using this.form to refer to the form and using the ! (not) operator.
    Code:
    <input type="checkbox" name="chk" onclick="this.form.sub.disabled=!this.checked" />

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Nice shortcut, glenngv

    object.attribute=!otherobject.attribute;

    I wonder if it works when value of the attribute is not a boolean, example if it is or it is not null
    Last edited by Kor; 05-30-2005 at 11:50 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    If you logical NOT a non-boolean value, the result will be a boolean. The non-boolean value will be internally casted to boolean and then it will be negated. See sample below.

    alert(!null); //true
    alert(!""); //true
    alert(!"blah"); //false
    alert(!"0"); //false

    These make no difference with these shortcuts that we usually use.

    var x=null;
    if (x) ...

    var y;
    if (y) ...


  •  

    Posting Permissions

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