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 2 of 2
  1. #1
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Disable form if checkbox is ticked

    Hello there everyone.

    I'm into PHP and Delphi dev so Javascript is a little over my head.

    Can anyone help me out with a simple little script?

    I have a form with two text fields and a checkbox

    |=======| - Textfield 1

    |=======| [-] - Textfield 2 and Checkbox 1

    If there is a value in Textfield 1, Checkbox 1 must be enabled. If there is no value, then it must be disabled.

    If there is a value in Textfield 1 and Checkbox 1 is checked then Textfield 2 must gain the value of Textfield 1 and both of the textfields must become disabled.

    If the box is again unchecked, the textfields must once again become enabled.

    Can anyone point me in the right direction? I am making a simple POS system for my business and this is for creation of an account. The checkbox would denote that the account name and the account holder are the same. [just so you know what it's going to be used for]

    I have very limited JS abilities so your help would be AWESOME!!

    Thanks a lot!!

    -Dr. Wong
    -DR.Wong

    Wheres the food at?

  • #2
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    Code:
    <form name="yourform">
    <input type="text" name="field1" onkeyup="checkCheck();">
    <br><input type="text" name="field2"><input type="checkbox" name="check" onclick="toggle()">
    </form>
    <script>
    var thingchecked=false;
    function toggle()
    {
    if(document.yourform.check.checked==true)
    {
    document.yourform.field2.value=document.yourform.field1.value;
    document.yourform.field1.disabled='disabled';
    document.yourform.field2.disabled='disabled';
    }
    else
    {
    document.yourform.field1.disabled='';
    document.yourform.field2.disabled='';
    }
    }
    toggle();
    document.yourform.check.checked=false;
    
    function checkCheck()
    {
    if (document.yourform.field1.value=="")
    {
    document.yourform.check.disabled='disabled';
    }
    else
    {
    document.yourform.check.disabled='';
    }
    }
    checkCheck()
    </script>
    You can edit your form and input names as you wish, just make sure you edit them in the code too.

    Hope this helps!
    ~binaryWeapon


  •  

    Posting Permissions

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