www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 09-29-2005, 03:29 PM
    endiz endiz is offline
    Registered User
     
    Join Date: Sep 2005
    Posts: 29
    Disable Checkbox array

    Hey guys, im trying to disable a checkbox array when I clock on a certain checkbox, but i can't seem to get the script to work.

    Here is the html, I didn't provide the script because its a _very_ big mess.

    Thanks guys in advance

    Code:
    <FORM>
    
    <INPUT TYPE=CHECKBOX NAME="head"   onClick="disable(this.form)">head<P>
    
    <INPUT TYPE=CHECKBOX NAME="headitem[]" VALUE="ip">ip<BR>
    <INPUT TYPE=CHECKBOX NAME="headitem[]" VALUE="host">host<BR>
    <INPUT TYPE=CHECKBOX NAME="headitem[]" VALUE="name">name<BR>
    <INPUT TYPE=CHECKBOX NAME="headitem[]" VALUE="id">id
    
    <P><INPUT TYPE=SUBMIT VALUE="submit">
    Reply With Quote
      #2  
    Old 09-29-2005, 04:18 PM
    Charles's Avatar
    Charles Charles is offline
    JavaScript Banned
     
    Join Date: Nov 2002
    Location: Baltimore, Maryland
    Posts: 11,667
    Checkboxes require differrent names. Make them unique and then try:
    Code:
    function disable (f) {
      var e, i = 1;
      while (e = f.elements[i++]) {if (e.type.toLowerCase() == 'checkbox') e.disabled = true}
    }
    If you really wanted radio buttons then you could use:
    Code:
    function disable (f) {
      var e, i = 0;
      while (e = f.getElementsByName ('headitem[]')[i++]) {e.disabled = true}
    }
    __________________
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

    Last edited by Charles; 09-29-2005 at 04:24 PM.
    Reply With Quote
      #3  
    Old 09-29-2005, 04:30 PM
    Kravvitz Kravvitz is offline
    CSS & JS/DOM Adept
     
    Join Date: Jul 2005
    Location: USA
    Posts: 3,913
    Here's another way.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title></title>
    <script type="text/javascript"><!--
    // IE4+, Firefox, Netscape 6+, Opera 6+
    function disable(cbx) {
      if(!cbx) return;
      var frm = cbx.form;
      for(var i=0;i<frm.elements.length;i++) {
        if(frm.elements[i].type && (frm.elements[i] != cbx)) {
          if(frm.elements[i].type.toLowerCase() == 'checkbox') {
            frm.elements[i].disabled = cbx.checked;
          }
        }
      }
    }
    // -->
    </script>
    </head>
    <body>
    
    <form name="form1" action="">
    
    <input type="checkbox" name="head" onclick="disable(this)">head<br><br>
    
    <input type="checkbox" name="headitem[]" value="ip">ip<br>
    <input type="checkbox" name="headitem[]" value="host">host<br>
    <input type="checkbox" name="headitem[]" value="name">name<br>
    <input type="checkbox" name="headitem[]" value="id">id
    
    <p><input type="submit" value="submit"></p>
    </form>
    
    </body>
    </html>
    Quote:
    Originally Posted by Charles
    Checkboxes require differrent names.
    Why? When named like that, it works fine, at least with a PHP form processor script.
    __________________
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

    Check out my blog.

    Last edited by Kravvitz; 09-29-2005 at 04:36 PM.
    Reply With Quote
      #4  
    Old 09-29-2005, 04:48 PM
    Charles's Avatar
    Charles Charles is offline
    JavaScript Banned
     
    Join Date: Nov 2002
    Location: Baltimore, Maryland
    Posts: 11,667
    I stand corrected. Thank you.

    From the HTML 4.01 Specification:
    Quote:
    Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property.
    __________________
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web
    Reply With Quote
      #5  
    Old 09-29-2005, 04:51 PM
    endiz endiz is offline
    Registered User
     
    Join Date: Sep 2005
    Posts: 29
    thanks Kravvitz, that was exactly what i was looking for.

    Any way i could reverse the process? (check enables them, and uncheck disables).

    Also, is it possible to make the head an array also?

    So lets say, I have multiple forms, but if each *head* is checked, it would enable that certain check box, and pass on with postdata that the header is checked, and each individual box is checked. Sound confusing? hehe, let me explain the whole project.


    I'm making a database site, with multiple search critirias. I want to enable each table to be selected within the mysql query, but in order to do that, i have to combine the tables, thus adding the checkbox's into an array.

    Basicly it will look something like this.

    Head()
    1name()
    1name()
    1name()
    1name()


    Head()
    2name()
    2name()
    2name()
    2name()

    Head()
    3name()
    3name()
    3name()
    3name().

    I realize i went very off track on this post, but I am just l ooking for guidance, I am a PHP\JS beginner, but I seem to be getting the hang of it =)

    Thanks guys.

    *edit*
    after i looked at my post, i realized I'm being very greedy. If its possible to just show me how to reverse the check I would appreciate it.
    *edit*

    Last edited by endiz; 09-29-2005 at 05:12 PM.
    Reply With Quote
      #6  
    Old 09-29-2005, 05:16 PM
    Kravvitz Kravvitz is offline
    CSS & JS/DOM Adept
     
    Join Date: Jul 2005
    Location: USA
    Posts: 3,913
    You're welcome

    Quote:
    Originally Posted by endiz
    Any way i could reverse the process? (check enables them, and uncheck disables).
    Change
    Code:
    frm.elements[i].disabled = cbx.checked;
    to
    Code:
    frm.elements[i].disabled = !cbx.checked;
    Quote:
    Originally Posted by endiz
    Also, is it possible to make the head an array also?
    Yes.
    __________________
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

    Check out my blog.
    Reply With Quote
      #7  
    Old 09-29-2005, 05:25 PM
    endiz endiz is offline
    Registered User
     
    Join Date: Sep 2005
    Posts: 29
    I love you buddy. I wish I could kiss you.

    Thank you very much. Much appreciate it. (both arrays work too).

    Last edited by endiz; 09-29-2005 at 05:58 PM.
    Reply With Quote
      #8  
    Old 09-30-2005, 10:17 AM
    endiz endiz is offline
    Registered User
     
    Join Date: Sep 2005
    Posts: 29
    Hey Krav, few more questions...

    What if i have multiple sections per form, and i want to disable each section per head checkbox. I tried to change your code around, but i seemed to just break it.

    the form now looks like this.
    Code:
    <form name="form1">
    
    <input type="checkbox" name="table[]" onclick="disable(this,head)" value="head">head<br><br>
    
    <input type="checkbox" name="headitem[]" value="ip" DISABLED>ip<br>
    <input type="checkbox" name="headitem[]" value="host" DISABLED>host<br>
    <input type="checkbox" name="headitem[]" value="name" DISABLED>name<br>
    <input type="checkbox" name="headitem[]" value="id" DISABLED>id
    
    <input type="checkbox" name="table[]" onclick="disable(this,nva)" value="nva">nva<br><br>
    
    <input type="checkbox" name="headitem[]" value="name" DISABLED>name<br>
    <input type="checkbox" name="headitem[]" value="date" DISABLED>date<br>
    <input type="checkbox" name="headitem[]" value="analyzed" DISABLED>analyzed<br>
    <input type="checkbox" name="headitem[]" value="email" DISABLED>email
    
    <p><input type="submit" value="submit"></p>
    </form>
    here was your last Javascript
    Code:
    <script type="text/javascript"><!--
    // IE4+, Firefox, Netscape 6+, Opera 6+
    function disable(cbx) {
      if(!cbx) return;
      var frm = cbx.form;
      for(var i=0;i<frm.elements.length;i++) {
        if(frm.elements[i].type && (frm.elements[i] != cbx)) {
          if(frm.elements[i].type.toLowerCase() == 'checkbox') {
            frm.elements[i].disabled = !cbx.checked;
          }
        }
      }
    }
    // -->
    </script>
    Thanks for all the help.

    Last edited by endiz; 09-30-2005 at 10:20 AM.
    Reply With Quote
      #9  
    Old 09-30-2005, 01:27 PM
    Charles's Avatar
    Charles Charles is offline
    JavaScript Banned
     
    Join Date: Nov 2002
    Location: Baltimore, Maryland
    Posts: 11,667
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <meta content="text/css" name="Content-Style-Type">
        <meta content="text/javascript" name="Content-Script-Type">
        <title>Example</title>    
    
        <script type="text/javascript">
        function toggle (o) {
          var e, i = 0
          while (e = o.parentNode.parentNode.getElementsByTagName ('INPUT')[i++]) {if (e.name == 'headitem[]') e.disabled = o.checked}
        }
        </script>
    
        <style type="text/css">
          form {width:24em; margin:auto}
          fieldset {margin:1ex; padding:1ex; width:10em}
          label {display:block}
          label.disable {margin-bottom:1em}
          button {display:block; margin:auto}      
        </style>
    
      </head>  
      <body>
        <form action="some-script.pl">
          <fieldset>
    
    	<legend>Head</legend>
    
            <label class="disable"><input type="checkbox" name="table[]" onclick="toggle (this)" value="head">Disable</label>
    
            <label><input type="checkbox" name="headitem[]" value="ip">ip</label>
            <label><input type="checkbox" name="headitem[]" value="host">host</label>
            <label><input type="checkbox" name="headitem[]" value="name">name</label>
            <label><input type="checkbox" name="headitem[]" value="id">id</label>
          </fieldset>
          <fieldset>
    
    	<legend>Nva</legend>
    
            <label class="disable"><input type="checkbox" name="table[]" onclick="toggle (this)" value="head">Disable</label>
    
            <label><input type="checkbox" name="headitem[]" value="ip">ip</label>
            <label><input type="checkbox" name="headitem[]" value="host">host</label>
            <label><input type="checkbox" name="headitem[]" value="name">name</label>
            <label><input type="checkbox" name="headitem[]" value="id">id</label>
          </fieldset>
    
          <div><button type="submit">Button</button></div>
        </form>
      </body>
    </html>
    __________________
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web
    Reply With Quote
      #10  
    Old 10-04-2005, 09:45 AM
    endiz endiz is offline
    Registered User
     
    Join Date: Sep 2005
    Posts: 29
    Perfect!
    Thanks guys!
    Reply With Quote
      #11  
    Old 10-04-2005, 11:49 AM
    endiz endiz is offline
    Registered User
     
    Join Date: Sep 2005
    Posts: 29
    Got another question:
    How would I implement another toggle script where it would disable and enable the appropriate textbox assossciated with that checkbox.

    Here is my code so far. Thanks guys, I'm learning alot.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <meta content="text/css" name="Content-Style-Type">
        <meta content="text/javascript" name="Content-Script-Type">
        <title>TEST</title>    
    
        <script type="text/javascript">
        function toggle (o) {
          var e, i = 0
          while (e = o.parentNode.parentNode.getElementsByTagName ('INPUT')[i++]) {if (e.name == 'headitem[]') e.disabled = !o.checked}
        }
        </script>
    
        <style type="text/css">
          form {width:24em; margin:auto}
          fieldset {margin:1ex; padding:1ex; width:20em}
          label {display:block}
          label.disable {margin-bottom:1em}
          button {display:block; margin:auto}      
        </style>
    
      </head>  
      <body>
        <form action="advresult.php">
          <fieldset>
    
    	<legend>Head<input type="checkbox" name="table[]" onclick="toggle (this)" value="head"></legend>
    
    
            <label><input type="checkbox" name="headitem[]" value="ip" DISABLED>ip<input type="text" name="query[]" maxlength=25 DISABLED></label>
            <label><input type="checkbox" name="headitem[]" value="host" DISABLED>host<input type="text" name="query[]" maxlength=25 DISABLED></label>
            <label><input type="checkbox" name="headitem[]" value="name" DISABLED>name<input type="text" name="query[]" maxlength=25 DISABLED></label>
            <label><input type="checkbox" name="headitem[]" value="id" DISABLED>id<input type="text" name="query[]" maxlength=25 DISABLED></label>
          </fieldset>
          <fieldset>
    
    	<legend>Nva<input type="checkbox" name="table[]" onclick="toggle (this)" value="NVA"></legend>
    
            
    
            <label><input type="checkbox" name="headitem[]" value="name" DISABLED>name<input type="text" name="query[]" maxlength=25 DISABLED></label>
            <label><input type="checkbox" name="headitem[]" value="date" DISABLED>date <input type="text" name="query[]" maxlength=25 DISABLED></label>
            <label><input type="checkbox" name="headitem[]" value="by" DISABLED>by <input type="text" name="query[]" maxlength=25 DISABLED></label>
            <label><input type="checkbox" name="headitem[]" value="analyzed" DISABLED>analyzed <input type="text" name="query[]" maxlength=25 DISABLED></label>
          </fieldset>
    
          <div><button type="submit">Search</button></div>
        </form>
      </body>
    </html>
    Reply With Quote
      #12  
    Old 10-04-2005, 04:44 PM
    endiz endiz is offline
    Registered User
     
    Join Date: Sep 2005
    Posts: 29
    Would i use the oncheck event? or still use the onclick event...
    Reply With Quote
      #13  
    Old 10-04-2005, 04:48 PM
    Kravvitz Kravvitz is offline
    CSS & JS/DOM Adept
     
    Join Date: Jul 2005
    Location: USA
    Posts: 3,913
    Quote:
    Originally Posted by endiz
    How would I implement another toggle script where it would disable and enable the appropriate textbox assossciated with that checkbox.
    Change the disabled property of the appropriate element. If you showed us an example pair of elements, I could be more specific.

    Quote:
    Originally Posted by endiz
    Would i use the oncheck event? or still use the onclick event...
    There is no oncheck event and IE's support for the onchange event on radio buttons and checkboxes is very buggy, so just use onclick.
    __________________
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

    Check out my blog.
    Reply With Quote
      #14  
    Old 10-04-2005, 04:52 PM
    endiz endiz is offline
    Registered User
     
    Join Date: Sep 2005
    Posts: 29
    I believe this is the pair.

    Code:
      <label><input type="checkbox" name="headitem[]" value="ip" DISABLED>ip<input type="text" name="query[]" maxlength=25 DISABLED></label>
    Reply With Quote
      #15  
    Old 10-04-2005, 05:09 PM
    Kravvitz Kravvitz is offline
    CSS & JS/DOM Adept
     
    Join Date: Jul 2005
    Location: USA
    Posts: 3,913
    You should not have multiple form controls associated with a single <label>.

    Quote:
    The LABEL element may be used to attach information to controls. Each LABEL element is associated with exactly one form control.
    http://www.w3.org/TR/html401/interac...tml#edef-LABEL

    Try this. I added the style="display:inline;" because you have the <label>s set to display:block.
    Code:
    <div><input type="checkbox" name="headitem[]" value="ip"
        onclick="this.parentNode.getElementsByTagName('input')[1].disabled=this.checked;"
        DISABLED><label style="display:inline;">ip<input type="text" name="query[]" maxlength=25 DISABLED></label></div>
    __________________
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

    Check out my blog.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 12:00 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.