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 4 of 4
  1. #1
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts

    Checkbox to activate text box in form

    I would like to have a checkbox and text box beside each other in a form. The text box would be greyed out and no info can be entered UNTIL the checkbox is checked. Once the checkbox is checked the text box becomes active and numbers then can be entered.

    Can this be done with javascript? If not what are my options?

    Thanks
    Leonard Whistler

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Your original question would have been just fine. Now you have gone and cross posted which is against the forum rules.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    function checkMe(){
    if(document.forms[0].thecheck.checked==1){
    	document.forms[0].test.disabled=false;
    	document.forms[0].test.value='1';
    }
    else if(document.forms[0].thecheck.checked==0){
    	document.forms[0].test.disabled=true;
    }
    }	
    </script>
    </head>
    
    <body>
    <form action="#" method="post">
    <input type="text" name="test" disabled="disabled" /><input type="checkbox" name="thecheck" onclick="checkMe()" />
    </form>
    </body>
    </html>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    While it can be done with javascript to enhance user experience, you, first, need to design a usable form without relying on javascript.

    Considering your post in HTML section, the checkbox seems to be a superfluous element. Why not default your quantity textboxes to 0 so that if a user wants an item he/she changes the text in that box to a different value. This scenario does not require client side scripting to be operational and reduces the amount of "clicks" user has to make to get the result.

    As far as that code above goes it is way too inefficient. Here is how you implement this approach (points of emphasis in red)
    Code:
    <script type="text/javasript">
    function buyThis(checkbox)
      {  // Use a sequence of nextSibling or previousSibling to reflect your markup 
         var textbox = checkbox.previousSibling;
         textbox.disabled = !checkbox.checked;
         textbox.value = checkbox.checked?'1':'0';
      }
    </script>
    
    <fieldset><legend>Products</legend>
    ....
    <input type="text" name="qty5" value="0" /><input type="checkbox" name="prod5" onchange="buyThis(this)" />
    ....
    </fieldset>
      }
    Last edited by Vladdy; 10-06-2005 at 07:38 AM. Reason: Saw that code above after posting....
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Ok...thanks for the replies in the morning I will be looking more into both suggestions. It's getting late.


    Re:"Now you have gone and cross posted which is against the forum rules."

    I always thought cross posting was cut & paste the exact same post within seconds in other forums. My original post was 1 hour or more before this one and then after some thought decided maybe the JavaScript section would be better, plus I rewrote it.
    Last edited by Len Whistler; 10-06-2005 at 07:35 AM.
    Leonard Whistler


  •  

    Posting Permissions

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