www.webdeveloper.com
Results 1 to 10 of 10

Thread: Disable form fields if checkbox is checked

Hybrid View

  1. #1
    Join Date
    May 2004
    Posts
    167

    Question Disable form fields if checkbox is checked

    I found a lot of example that allow disabling of form elements on checkbox click, but I actually can have a page loaded with a checkbox selected.

    I suppose I need some function that will check if the checkbox (let's call it chBox) checked onload and if yes -- disable the fields. If not leave them active. Also I'll need to add onchange statement to my checkbox to enable/disable form elements.

    JS is a bit confusing since i'm more familiar with server-side coding...

  2. #2
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    Is this what you are looking for?
    Code:
    <form name="myform">
    <input type="checkbox" name="mycheck" checked="true">
    </form>
    To check/uncheck:
    Code:
    <script language="javascript">
    <!--
    function togglethebox(whatbox) {
    var checked = whatbox.checked;
      if (checked == true) { whatbox.checked = false; }
      else { whatbox.checked = true; }
    }
    -->
    </script>
    Last edited by savvykms; 10-30-2007 at 02:50 PM.

  3. #3
    Join Date
    May 2004
    Posts
    167
    No, not quite.

    Let's say I have couple other form fields on the page. If checked they need to be disabled if not checked -- active.

  4. #4
    Join Date
    May 2004
    Posts
    167
    Ok, I just cooked up something and ran into another problem. First here's what i came up with:
    PHP Code:
    <script type="text/javascript">
    function 
    enableDisable() {
      if(
    document.myForm.myCheckbox.checked){
         
    document.myForm.myTextfield.disabled true;
      } else {
         
    document.myForm.myTextfield.disabled false;
      }
    }
    window.onload enableDisable;
    </script> 
    Nothing sexy, but works.

  5. #5
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    I whipped up an all-purpose one that evaluates parameter text strings.
    Syntax: toggleform('theform','checkbox','textbox1,textbox2')
    Javascript functions:
    Code:
    <script language="javascript">
    <!--
    function enableob(o) { eval(o+".disabled = false"); }
    function disableob(o) { eval(o+".disabled = true"); }
    function toggleform(formstr,chkobstr,obstr) {
    var checked = eval(formstr+"."+chkobstr+".checked");
    var obs = obstr.split(",");
      for (i = 0; i < obs.length; i++) {
      obs[i] = formstr+"."+obs[i];
      }
      if (checked == false) {
        for (i = 0; i < obs.length; i++) {
        disableob(obs[i]);
        }
      }
      else {
        for (i = 0; i < obs.length; i++) {
        enableob(obs[i]);
        }
      }
    }
    -->
    </script>
    HTML:
    Code:
    <noscript><p>You do not have scripting enabled and cannot use the below form because of it.</p></noscript>
    <form name="myform">
    <p>To accept the license terms for posting, click the checkbox below:<br>
    <input type="checkbox" name="mycheck" onclick="toggleform('document.myform','mycheck','pname,ptext')"> I Accept</p>
    <b>Name:</b> <input type="textbox" value="your nickname" name="pname" disabled="true"><br>
    <b>Post:</b><br>
    <textarea name="ptext" disabled="true">Your post here</textarea>

  6. #6
    Join Date
    May 2004
    Posts
    167
    Oh, cool. I'll give it a whirl. Thanks!

  7. #7
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    No problem at all. I started to learn web design like 2-3 years back, and I am only 16 years old... god damn I think its funny when people have trouble with it, but maybe that comes from my programming experience... Anything else?

  8. #8
    Join Date
    May 2004
    Location
    FL
    Posts
    3,447
    savvykms, I like your script. Mind if I post it over at JavaScript Source? I'll give you the proper credit, of course. I just need the name and URL you want to use.
    Lee

    ""Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."" -- Sir Winston Churchill

  9. #9
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    I have made many like it, very nice control scripts etc. I think my only problem is keeping up with broswer compatibility. Luckily I now use Firefox as well. You know what Macintosh users? f*ck you all! Same with anyone who uses Netscape or Safari!

    Now that that is over, if anyone on this forum wishes to use my scripts, feel free, credit or no credit (I would perfer credit). Please add in a email link if you give credit to savvykms[a]aim[d]com. Maybe use this:

    /*Credit goes to Savvy (email: savvykms[a]aim[d]com) for this script. Do not email to this address asking for help. You may use this script freely, but include this notice if you do.*/

    Some examples of things I have made:
    * 3-4 TicTacToe games (AI or player vs player)
    * connect four
    * 5 or more attempts at text-based games (press the attack button, have items, attacks, health, cheats etc)
    * 2-3 fancy calculators
    * 2 or more customize web broswer attempts
    * 2 or more search engines
    * customized email links (fills in subject body etc with values from a simple anchor or button)
    * remote control windows
    * popup spammers (hehe, just for fun, crashes computers)
    * text editors (HTML Applications (HTA))
    * Quake2 Gloom control maker (HTA, uses filesystem to save config files)
    * more
    non-javascript
    PHP: forums, games, online chat scripts, dynamic images, search engine (was called Web Savvy and looks similar to Google and works same basic way, was only published for a short while)

    Link may be outdated: http://www.webdeveloper.com/forum/sh...286#post819286 <-- has a fun solution
    Last edited by savvykms; 10-30-2007 at 03:57 PM.

  10. #10
    Join Date
    May 2004
    Posts
    167

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles