Disable some checkboxes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Bedford

    #1

    Disable some checkboxes

    I create checkboxes using datas from a database (with PHP).
    I store all values in an array, as I must pass this value like a Form value.

    Now, in some cases, when a checkbox is selected, I must disactivate other
    checkboxes.

    How can I do this ?

    here is the code:

    print("<TR><TD valign=\"top\"> <input type=\"checkbox \" name=\"details[]\"
    value=\"".$row_ Recordset->ID."\"></TD><TD
    valign=\"top\"> ".$row_Recordse t->Description) ;

    here is the output: [] is for checkbox
    [] Blue
    []
    []
    []
    []



  • Martin Honnen

    #2
    Re: Disable some checkboxes



    Bob Bedford wrote:
    [color=blue]
    > I create checkboxes using datas from a database (with PHP).
    > I store all values in an array, as I must pass this value like a Form value.
    >
    > Now, in some cases, when a checkbox is selected, I must disactivate other
    > checkboxes.
    >
    > How can I do this ?
    >
    > here is the code:
    >
    > print("<TR><TD valign=\"top\"> <input type=\"checkbox \" name=\"details[]\"
    > value=\"".$row_ Recordset->ID."\"></TD><TD
    > valign=\"top\"> ".$row_Recordse t->Description) ;[/color]

    Here is an example:

    <html lang="en">
    <head>
    <title>checkb ox disabling</title>
    <script type="text/javascript">
    function disableHandler (form, inputName) {
    var inputs = form.elements[inputName];
    for (var i = 0; i < inputs.length; i++) {
    var input = inputs[i];
    input.onclick = function (evt) {
    if (this.checked) {
    disableInputs(t his, inputs);
    }
    else {
    enableInputs(th is, inputs);
    }
    return true;
    };
    }
    }

    function disableInputs (input, inputs) {
    for (var i = 0; i < inputs.length; i++) {
    var currentInput = inputs[i];
    if (currentInput != input) {
    currentInput.di sabled = true;
    }
    }
    }

    function enableInputs (input, inputs) {
    for (var i = 0; i < inputs.length; i++) {
    var currentInput = inputs[i];
    if (currentInput != input) {
    currentInput.di sabled = false;
    }
    }
    }
    </script>
    </head>
    <body>
    <form name="aForm" action="">
    <p>
    <label>
    Blue
    <input type="checkbox" name="details[]" value="blue">
    </label>
    <label>
    Green
    <input type="checkbox" name="details[]" value="green">
    </label>
    <label>
    Red
    <input type="checkbox" name="details[]" value="red">
    </label>
    </p>
    </form>
    <script type="text/javascript">
    disableHandler( document.forms. aForm, 'details[]');
    </script>
    </body>
    </html>

    Disabling form controls works in browsers like Netscape 6/7, IE4+, Opera
    7 but not old ones like Netscape 4.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Bob Bedford

      #3
      Re: Disable some checkboxes

      Thanks for your code, Martin, but all checkboxes belong to the same array
      (details[]) but this is not for grouping: the groups are different:

      //1st group (colors)
      [] Blue
      [] Green
      [] Red
      [] Purple
      //2nd group (size)
      [] Big
      [] Small
      //3rd group (weight)
      [] Heavy
      [] Light

      So clicking on any color item, it should disable all other colors items, but
      not size and weight items
      Also, one more difficulty, any item may be in different groups, so clicking
      on any will disable all of his group.

      So forget the previous example and take this better one: (all items must
      have the name="details[]").
      [] item1 (belong to group 1,2,4)
      [] item2 (belong to group 1,2,4)
      [] item3 (belong to group 1,2,4)
      [] item4 (belong to group 1,3)
      [] item5 (belong to group 3)
      [] item6 (belong to group 4)

      So:
      clicking on item1, disable items 2,3,4,6
      clicking on item2, disable items 1,3,4,5
      clicking on item3, disable items 1,2,4,5
      clicking on item4, disable items 1,2,3,5
      clicking on item5, disable items 4 only
      clicking on item6, disable items 1,2,3

      I know it's quite complex !!!! how to do so ????

      BoB



      Comment

      • Martin Honnen

        #4
        Re: Disable some checkboxes



        Bob Bedford wrote:

        [color=blue]
        > So forget the previous example and take this better one: (all items must
        > have the name="details[]").
        > [] item1 (belong to group 1,2,4)
        > [] item2 (belong to group 1,2,4)
        > [] item3 (belong to group 1,2,4)
        > [] item4 (belong to group 1,3)
        > [] item5 (belong to group 3)
        > [] item6 (belong to group 4)
        >
        > So:
        > clicking on item1, disable items 2,3,4,6
        > clicking on item2, disable items 1,3,4,5
        > clicking on item3, disable items 1,2,4,5
        > clicking on item4, disable items 1,2,3,5
        > clicking on item5, disable items 4 only
        > clicking on item6, disable items 1,2,3
        >
        > I know it's quite complex !!!! how to do so ????[/color]

        Well there is the onclick handler to attach code to be called when the
        input is clicked, the checked property to find out whether the checkbox
        is checked and the disabled property to disable a property so you need
        <input type="checkbox"
        name="details[]"
        onclick="if (this.checked) {
        //disable needed inputs
        this.form.eleme nts[this.name][1].disabled = true;
        }
        else {
        //enabled needed inputs e.g.
        this.form.eleme nts[this.name][1].disabled = false;
        }
        return true;">

        It is up to you now to write down the code for the different checkboxes.

        --

        Martin Honnen
        http://JavaScript.FAQTs.com/

        Comment

        • Bob Bedford

          #5
          Re: Disable some checkboxes


          "Martin Honnen" <mahotrash@yaho o.de> a écrit dans le message de
          news:40e3e81d$1 @olaf.komtel.ne t...[color=blue]
          >
          >
          > Bob Bedford wrote:
          >
          >[color=green]
          > > So forget the previous example and take this better one: (all items must
          > > have the name="details[]").
          > > [] item1 (belong to group 1,2,4)
          > > [] item2 (belong to group 1,2,4)
          > > [] item3 (belong to group 1,2,4)
          > > [] item4 (belong to group 1,3)
          > > [] item5 (belong to group 3)
          > > [] item6 (belong to group 4)
          > >
          > > So:
          > > clicking on item1, disable items 2,3,4,6
          > > clicking on item2, disable items 1,3,4,5
          > > clicking on item3, disable items 1,2,4,5
          > > clicking on item4, disable items 1,2,3,5
          > > clicking on item5, disable items 4 only
          > > clicking on item6, disable items 1,2,3
          > >
          > > I know it's quite complex !!!! how to do so ????[/color]
          >
          > Well there is the onclick handler to attach code to be called when the
          > input is clicked, the checked property to find out whether the checkbox
          > is checked and the disabled property to disable a property so you need
          > <input type="checkbox"
          > name="details[]"
          > onclick="if (this.checked) {
          > //disable needed inputs
          > this.form.eleme nts[this.name][1].disabled = true;
          > }
          > else {
          > //enabled needed inputs e.g.
          > this.form.eleme nts[this.name][1].disabled = false;
          > }
          > return true;">
          >
          > It is up to you now to write down the code for the different checkboxes.[/color]

          How can I add an array to any details item and then go trough any item when
          a box is checked ?

          article[0] = item1 (value=123, exclude=(233,43 2,456))
          article[1] = item2 (value=233, exclude=(432,45 6))
          article[2] = item3 (value=455, exclude=(457,47 7,874))
          ....
          so clicking on any item, it should look at his exclude list, and then go
          trough all article's item and retrieve the one wich value = any of the
          existing exclude number and disable it! also the ability to enable it again
          if the article is unchecked.

          How to do so in Javascript ?



          Comment

          • Martin Honnen

            #6
            Re: Disable some checkboxes



            Bob Bedford wrote:

            [color=blue]
            > How can I add an array to any details item and then go trough any item when
            > a box is checked ?[/color]
            [color=blue]
            > How to do so in Javascript ?[/color]

            The JavaScript docs are at

            the Array reference at

            there is also a guide at

            treating arrays here

            Now try to write some code yourself


            --

            Martin Honnen
            http://JavaScript.FAQTs.com/

            Comment

            Working...