How to check all the checkboxes if checkbox name is 'name[]'

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

    How to check all the checkboxes if checkbox name is 'name[]'

    Hi, since sometime I'm stuck in a problem where I want to check or
    uncheck all the checkboxes. If I'm choosing name for the checkbox
    array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes
    (code pasted below). But if name of the checkbox array is
    'chkbx_ary[]' then it's failing. I want the name to be 'chkbx_ary[]'
    because I want to access this array at server side.
    Though one may not require to see php part but I'm still pasting the
    code.

    <script language="JavaS cript" type="text/javascript">
    function CheckAll(field)
    {
    for (loop=0; loop < field.length; loop++)
    {
    field[loop].checked = true;
    }
    }

    function UnCheckAll(fiel d)
    {
    for (loop=0; loop < field.length; loop++)
    {
    field[loop].checked = false;
    }
    }
    </script>

    <?php
    echo '<form name="TestSelec t_Form" method="GET" action="TestLis t.php"
    enctype="text/plain" style="margin:0 px">
    <input type="radio" name="SelectOrN ot" value="Select_A ll"
    onClick="CheckA ll(document.Tes tSelect_Form.Se lectModule)"
    style="position :absolute;left: 290px;top:140px ;z-index:6">
    <input type="radio" name="SelectOrN ot" value="Uncheck_ All"
    onClick="UnChec kAll(document.T estSelect_Form. SelectModule)" checked
    style="position :absolute;left: 400px;top:140px ;z-index:7">';
    $file_name = "CONF/testselect.txt" ;
    $fp_TstSlct = fopen("$file_na me", "r") or die("Some error occurred
    while opening 'testSelect.txt ");
    $TopPos = 170;
    while(!feof($fp _TstSlct))
    {
    $TstSlct_data = fgets($fp_TstSl ct);
    preg_match_all( "/([#]*)(.*)\s(\d+)\s *[;#]*(.*)/", $TstSlct_data,
    $match, PREG_PATTERN_OR DER);
    if ( $match[2][0] != NULL ) { //Checking whether module is present
    or not. May be only comments have been put. For. e.g.
    ############### ####DSL HOME#########
    if ( $match[1][0] == NULL ) { //Checking if line is starting with
    '#' or not
    echo '<input type="checkbox" name="SelectMod ule[]" value=' .
    $match[2][0] . ' checked="checke d" style="position :absolute;left:
    90px;top:' . $TopPos . 'px">';
    } else {
    echo '<input type="checkbox" name="SelectMod ule[]" value=' .
    $match[2][0] . ' style="position :absolute;left: 90px;top:' . $TopPos .
    'px">';
    }

    echo '<div id="text3" style="position :absolute; overflow:hidden ;
    left:130px; top:' . $TopPos . 'px; width:79px; height:16px"><d iv
    class="wpmd">';
    echo '<div><font face="Arial">' . $match[2][0] . '</font></div>';
    echo '</div></div>';

    $TopPos = $TopPos + 25;
    }
    }
    fclose($fp_TstS lct);

    echo '<input name="Configure TestList" type="submit" value="Next"
    style="position :absolute;left: 326px;top:403px ;z-index:16">
    </form>

    <div id="text1" style="position :absolute; overflow:hidden ; left:
    265px; top:118px; width:69px; height:18px; z-index:9"><div
    class="wpmd">
    <div><font class="ws12" face="Arial">Se lect All</font></div>
    </div></div>

    <div id="text2" style="position :absolute; overflow:hidden ; left:
    371px; top:118px; width:85px; height:18px; z-index:10"><div
    class="wpmd">
    <div><font class="ws12" face="Arial">Un check All</font></div>
    </div></div>';
    ?>

    Please someone suggest the solution as I'm totally stuck.
    Thanks a lot in advance.
  • sheldonlg

    #2
    Re: How to check all the checkboxes if checkbox name is 'name[]'

    PhpCool wrote:
    Hi, since sometime I'm stuck in a problem where I want to check or
    uncheck all the checkboxes. If I'm choosing name for the checkbox
    array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes
    (code pasted below). But if name of the checkbox array is
    'chkbx_ary[]' then it's failing. I want the name to be 'chkbx_ary[]'
    because I want to access this array at server side.
    Though one may not require to see php part but I'm still pasting the
    code.
    How about?
    obj = document.getEle mentsByName('ch kbx_ary[]'); obj[i].value=somethin g;

    I know this works because in one of my apps I have "n" similar rows, all
    with checkboxes named cond[] (for the same reason -- php). I have a
    clear button at the end of each row where I call a javascript function
    passing in the id number, i. In that function I use:

    obj = document.getEle mentsByName('co nd[]'); obj[i].value=0;

    It should be a simple matter for you to do:

    obj = document.getEle mentsByName('ch kbx_ary[]');
    for (i=0; i<obj.length; i++) {
    obj[i].value = something;
    }


    Comment

    • Stevo

      #3
      Re: How to check all the checkboxes if checkbox name is 'name[]'

      PhpCool wrote:
      If I'm choosing name for the checkbox
      array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes
      (code pasted below). But if name of the checkbox array is
      'chkbx_ary[]' then it's failing. I want the name to be 'chkbx_ary[]'
      because I want to access this array at server side.
      The characters [ and ] aren't valid in a name. You can use a-z, A-Z and
      0-9 and underscore. That's it.

      Comment

      • Conrad Lender

        #4
        Re: How to check all the checkboxes if checkbox name is 'name[]'

        On 2008-10-28 13:29, Stevo wrote:
        The characters [ and ] aren't valid in a name. You can use a-z, A-Z and
        0-9 and underscore. That's it.
        If you're talking about the HTML "name" attribute, that's not correct.
        The content of "name" is defined as cdata, which includes the square
        brackets. <input name="box[]" type="checkbox" value="foo"is valid.

        If you're talking about ECMAScript identifiers, it's not correct either.
        According to the standard, almost any Unicode letter (or corresponding
        escape sequence) can be used in an Identifier:

        Identifier ::
        IdentifierName but not ReservedWord

        IdentifierName ::
        IdentifierStart
        IdentifierName IdentifierPart

        IdentifierStart ::
        UnicodeLetter
        $
        _
        \ UnicodeEscapeSe quence

        UnicodeLetter
        any character in the Unicode categories “Uppercase letter (Lu)”,
        “Lowercase letter (Ll)”, “Titlecase letter (Lt)”,
        “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number
        (Nl)”.


        This is valid ECMAScript:

        var привет = "hello";
        alert(привРµÑ‚);

        I found that rather surprising, but it appears to work at least in
        Firefox and Opera, and in Rhino:

        jsvar привет = "hello";
        jsprint(приР²ÐµÑ‚);
        hello

        It does not work with the SpiderMonkey jsshell:

        jsvar привет = "hello";
        typein:1: SyntaxError: illegal character:
        typein:1: var привет = "hello";
        typein:1: .....^
        jsvar hélłö = "hello";
        typein:2: SyntaxError: illegal character:
        typein:2: var hélłö = "hello";
        typein:2: ......^


        So, all in all it's probably not a good idea to rely on it, and stick to
        ASCII characters in identifiers.


        - Conrad

        Comment

        • Matt Kruse

          #5
          Re: How to check all the checkboxes if checkbox name is 'name[]'

          On Oct 28, 7:29 am, Stevo <n...@mail.inva lidwrote:
          The characters [ and ] aren't valid in a name. You can use a-z, A-Z and
          0-9 and underscore. That's it.
          That's an old misunderstandin g, dis-proven many times. Please don't
          keep spreading it.

          Matt Kruse

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: How to check all the checkboxes if checkbox name is 'name[]'

            Conrad Lender wrote:
            On 2008-10-28 13:29, Stevo wrote:
            >The characters [ and ] aren't valid in a name. You can use a-z, A-Z and
            >0-9 and underscore. That's it.
            >
            [...]
            If you're talking about ECMAScript identifiers, it's not correct either.
            According to the standard, almost any Unicode letter (or corresponding
            escape sequence) can be used in an Identifier:
            That's nonsense, at best misleading.
            Identifier ::
            IdentifierName but not ReservedWord
            >
            IdentifierName ::
            IdentifierStart
            IdentifierName IdentifierPart
            >
            IdentifierStart ::
            UnicodeLetter
            $
            _
            \ UnicodeEscapeSe quence
            >
            UnicodeLetter
            any character in the Unicode categories “Uppercase letter (Lu)”,
            “Lowercase letter (Ll)”, “Titlecase letter (Lt)”,
            “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number
            (Nl)”.
            Note that "[" and "]" do _not_ occur and are _not_ contained in the
            mentioned Unicode subsets; only letters are allowed for good reason. You
            may include these characters in identifiers escaped as \u005b and \u005d,
            but not as they are. A wise design decision, otherwise

            f[oo]][][][b][a][r][]]

            would confuse the hell out of the parser ;-)
            This is valid ECMAScript:
            >
            var привет = "hello";
            alert(привРµÑ‚);
            >
            I found that rather surprising, but it appears to work at least in
            Firefox and Opera, and in Rhino:
            [...]
            It does not work with the SpiderMonkey jsshell:
            >
            jsvar привет = "hello";
            typein:1: SyntaxError: illegal character:
            typein:1: var привет = "hello";
            typein:1: .....^
            [...]
            So, all in all it's probably not a good idea to rely on it,
            It *might not* be a good idea to rely on it, especially if older
            implementations such as JavaScript 1.2/1.3 of Netscape 4.x should be
            considered. Certainly your SpiderMonkey build not having Unicode support
            compiled in (for whatever reason) provides no valid argument at all in favor
            of avoiding Unicode identifiers. Obviously it is compiled into the version
            implemented in Gecko.
            and stick to ASCII characters in identifiers.
            A property *name* does not need to be an identifier:

            var foo = {"bar[]": "baz"};
            foo["bar[]"] = 42;

            Hint: `this' can be the base object of a property access.


            PointedEars
            --
            Anyone who slaps a 'this page is best viewed with Browser X' label on
            a Web page appears to be yearning for the bad old days, before the Web,
            when you had very little chance of reading a document written on another
            computer, another word processor, or another network. -- Tim Berners-Lee

            Comment

            • sheldonlg

              #7
              Re: How to check all the checkboxes if checkbox name is 'name[]'

              Stevo wrote:
              PhpCool wrote:
              >If I'm choosing name for the checkbox
              >array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes
              >(code pasted below). But if name of the checkbox array is
              >'chkbx_ary[]' then it's failing. I want the name to be 'chkbx_ary[]'
              >because I want to access this array at server side.
              >
              The characters [ and ] aren't valid in a name. You can use a-z, A-Z and
              0-9 and underscore. That's it.
              ....as I said, I used cond[] as a name and it worked perfectly for me.

              Comment

              • Conrad Lender

                #8
                Re: How to check all the checkboxes if checkbox name is 'name[]'

                On 2008-10-29 02:13, Thomas 'PointedEars' Lahn wrote:
                >According to the standard, almost any Unicode letter (or
                >correspondin g escape sequence) can be used in an Identifier:
                >
                That's nonsense, at best misleading.
                No, it's correct. Please re-read what I actually wrote. I said "almost
                any Unicode *letter*", not character. There's a difference.
                >UnicodeLette r
                > any character in the Unicode categories “Uppercase letter (Lu)”,
                > “Lowercase letter (Ll)”, “Titlecase letter (Lt)”,
                > “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number
                > (Nl)”.
                >
                Note that "[" and "]" do _not_ occur and are _not_ contained in the
                mentioned Unicode subsets;
                I never said they were.
                >So, all in all it's probably not a good idea to rely on it,
                >
                It *might not* be a good idea to rely on it, especially if older
                implementations such as JavaScript 1.2/1.3 of Netscape 4.x should be
                considered. Certainly your SpiderMonkey build not having Unicode
                support compiled in (for whatever reason) provides no valid argument
                at all in favor of avoiding Unicode identifiers. Obviously it is
                compiled into the version implemented in Gecko.
                Yeah, I had some trouble compiling it, and you're right, the quick test
                with jsshell didn't prove much. I only tried it out of curiosity (and
                because I was too lazy to start the Windows VM for IE and Safari).

                [a little OT SpiderMonkey detour] The default compile went without a
                hitch, but when I tried to use different configurations (like adding
                support for the old File object), it always failed. The suggestions I
                found in m.d.t.js-engine didn't work for me, but I found out that the
                stand-alone SpiderMonkey interpreter probably wasn't what I was looking
                for anyway. Somebody suggested JSDB, which embeds the SpiderMonkey
                engine and comes with an assortment of useful host objects. I asked
                about that here a few days ago, but haven't found the time to try it out
                yet. [end detour :-)]
                A property *name* does not need to be an identifier:
                >
                var foo = {"bar[]": "baz"}; foo["bar[]"] = 42;
                >
                Hint: `this' can be the base object of a property access.
                Thanks, I'm aware of that, and I was specifically talking about
                identifiers. But the OP will be interested in this snippet.


                - Conrad


                PS: While I'm already off-topic, could I ask you a personal favor? You
                have a habit of starting your replies with "Nonsense." I personally find
                it very rude to summarily dismiss another poster's opinion as nonsense.
                It comes very close to an insult. I don't know if this is some kind of
                language-barrier thing (unlikely because your English is practically
                indistinguishab le from a native speaker; but it's possible). I like
                talking to you, and I value your insightful comments. I've made my share
                of mistakes, as have we all, and I'm always grateful for corrections,
                but your "Nonsense" tags only serve to make me defensive and angry. I
                wish you could find a different, more polite form of expressing your
                disagreement - especially when you see that the person you're replying
                to has actually tried to make a logical argument. Sure, I can "stand the
                heat", as you've described it before. I just don't see any reason for
                unprovoked fire and flames in a technical discussion. It's your choice,
                of course, but you should be aware of how it will be received.

                Comment

                • David Mark

                  #9
                  Re: How to check all the checkboxes if checkbox name is 'name[]'

                  On Oct 28, 10:59 pm, Conrad Lender <crlen...@yahoo .comwrote:
                  On 2008-10-29 02:13, Thomas 'PointedEars' Lahn wrote:
                  [snip]
                  >
                  PS: While I'm already off-topic, could I ask you a personal favor? You
                  You should have done that by email.
                  have a habit of starting your replies with "Nonsense." I personally find
                  it very rude to summarily dismiss another poster's opinion as nonsense.
                  It comes very close to an insult. I don't know if this is some kind of
                  Personally, I don't condone such behavior, but more often than not, he
                  is calling a spade a spade.

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: How to check all the checkboxes if checkbox name is 'name[]'

                    Thomas 'PointedEars' Lahn wrote:
                    Conrad Lender wrote:
                    >On 2008-10-28 13:29, Stevo wrote:
                    >>The characters [ and ] aren't valid in a name. You can use a-z, A-Z and
                    >>0-9 and underscore. That's it.
                    >[...]
                    >If you're talking about ECMAScript identifiers, it's not correct either.
                    >According to the standard, almost any Unicode letter (or corresponding
                    >escape sequence) can be used in an Identifier:
                    >
                    That's nonsense, at best misleading.
                    Oops: *letter*. Sorry, so that's not nonsense; but it is misleading because
                    identifiers are not names.


                    PointedEars
                    --
                    Prototype.js was written by people who don't know javascript for people
                    who don't know javascript. People who don't know javascript are not
                    the best source of advice on designing systems that use javascript.
                    -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

                    Comment

                    • Suhas Dhoke

                      #11
                      Re: How to check all the checkboxes if checkbox name is 'name[]'

                      On Oct 28, 4:28 pm, PhpCool <goyal.anujku.. .@gmail.comwrot e:
                      Hi, since sometime I'm stuck in a problem where I want to check or
                      uncheck all the checkboxes. If I'm choosing name for the checkbox
                      array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes
                      (code pasted below). But if name of the checkbox array is
                      'chkbx_ary[]' then it's failing. I want the name to be 'chkbx_ary[]'
                      because I want to access this array at server side.
                      Though one may not require to see php part but I'm still pasting the
                      code.
                      >
                      <script language="JavaS cript" type="text/javascript">
                              function CheckAll(field)
                              {
                                      for (loop=0; loop < field.length; loop++)
                                      {
                                              field[loop].checked = true;
                                      }
                              }
                      >
                              function UnCheckAll(fiel d)
                              {
                                      for (loop=0; loop < field.length; loop++)
                                      {
                                              field[loop].checked = false;
                                      }
                              }
                      </script>
                      >
                      <?php
                              echo '<form name="TestSelec t_Form" method="GET" action="TestLis t.php"
                      enctype="text/plain" style="margin:0 px">
                                      <input type="radio" name="SelectOrN ot" value="Select_A ll"
                      onClick="CheckA ll(document.Tes tSelect_Form.Se lectModule)"
                      style="position :absolute;left: 290px;top:140px ;z-index:6">
                                      <input type="radio" name="SelectOrN ot" value="Uncheck_ All"
                      onClick="UnChec kAll(document.T estSelect_Form. SelectModule)" checked
                      style="position :absolute;left: 400px;top:140px ;z-index:7">';
                              $file_name = "CONF/testselect.txt" ;
                              $fp_TstSlct = fopen("$file_na me", "r") or die("Some error occurred
                      while opening 'testSelect.txt ");
                              $TopPos = 170;
                              while(!feof($fp _TstSlct))
                              {
                                      $TstSlct_data = fgets($fp_TstSl ct);
                                      preg_match_all( "/([#]*)(.*)\s(\d+)\s *[;#]*(.*)/", $TstSlct_data,
                      $match, PREG_PATTERN_OR DER);
                                      if ( $match[2][0] != NULL ) {   //Checking whether module is present
                      or not. May be only comments have been put. For. e.g.
                      ############### ####DSL HOME#########
                                              if ( $match[1][0] == NULL ) {   //Checking if line is starting with
                      '#' or not
                                                      echo '<input type="checkbox" name="SelectMod ule[]" value=' .
                      $match[2][0] . ' checked="checke d" style="position :absolute;left:
                      90px;top:' . $TopPos . 'px">';
                                              } else {
                                                      echo '<input type="checkbox" name="SelectMod ule[]" value=' .
                      $match[2][0] . ' style="position :absolute;left: 90px;top:' . $TopPos .
                      'px">';
                                              }
                      >
                                              echo '<div id="text3" style="position :absolute; overflow:hidden ;
                      left:130px; top:' . $TopPos . 'px; width:79px; height:16px"><d iv
                      class="wpmd">';
                                              echo '<div><font face="Arial">' . $match[2][0] . '</font></div>';
                                              echo '</div></div>';
                      >
                                              $TopPos = $TopPos + 25;
                                      }
                              }
                              fclose($fp_TstS lct);
                      >
                              echo '<input name="Configure TestList" type="submit" value="Next"
                      style="position :absolute;left: 326px;top:403px ;z-index:16">
                              </form>
                      >
                              <div id="text1" style="position :absolute; overflow:hidden ; left:
                      265px; top:118px; width:69px; height:18px; z-index:9"><div
                      class="wpmd">
                              <div><font class="ws12" face="Arial">Se lect All</font></div>
                              </div></div>
                      >
                              <div id="text2" style="position :absolute; overflow:hidden ; left:
                      371px; top:118px; width:85px; height:18px; z-index:10"><div
                      class="wpmd">
                              <div><font class="ws12" face="Arial">Un check All</font></div>
                              </div></div>';
                      ?>
                      >
                      Please someone suggest the solution as I'm totally stuck.
                      Thanks a lot in advance.
                      Hello PhpCool.

                      I've modified your code, and here is the updated version..

                      #####
                      <script language="JavaS cript" type="text/javascript">
                      function checkAll (rad, selectType) {
                      for (var i=0;i < document.TestSe lect_Form.eleme nts.length;i++) {
                      var e = document.TestSe lect_Form.eleme nts[i];
                      if ('checkbox' == e.type && 'select' == selectType) {
                      e.checked = true;
                      } else {
                      e.checked = false;
                      }
                      }
                      rad.checked = true;
                      }
                      </script>

                      <?php
                      echo '<form name="TestSelec t_Form" method="GET"
                      action="TestLis t.php" enctype="text/plain" style="margin:0 px">
                      <input type="radio" name="SelectOrN ot" value="Select_A ll"
                      onClick="checkA ll(this, \'select\');" style="position :absolute;left:
                      290px;top:140px ;z-index:6">
                      <input type="radio" name="SelectOrN ot" value="Uncheck_ All"
                      onClick="checkA ll(this, \'unselect\');" checked
                      style="position :absolute;left: 400px;top:140px ;z-index:7">';
                      #####

                      Hope this code will work for you.
                      Let me know.

                      Enjoy.

                      Comment

                      • khaled.jouda@gmail.com

                        #12
                        Re: How to check all the checkboxes if checkbox name is 'name[]'

                        On Oct 28, 12:28 pm, PhpCool <goyal.anujku.. .@gmail.comwrot e:
                        Hi, since sometime I'm stuck in a problem where I want to check or
                        uncheck all the checkboxes. If I'm choosing name for the checkbox
                        array as 'chkbx_ary' then I'm able to check/uncheck all the checkboxes
                        (code pasted below). But if name of the checkbox array is
                        'chkbx_ary[]' then it's failing. I want the name to be 'chkbx_ary[]'
                        because I want to access this array at server side.
                        Though one may not require to see php part but I'm still pasting the
                        code.
                        >
                        <script language="JavaS cript" type="text/javascript">
                                function CheckAll(field)
                                {
                                        for (loop=0; loop < field.length; loop++)
                                        {
                                                field[loop].checked = true;
                                        }
                                }
                        >
                                function UnCheckAll(fiel d)
                                {
                                        for (loop=0; loop < field.length; loop++)
                                        {
                                                field[loop].checked = false;
                                        }
                                }
                        </script>
                        >
                        <?php
                                echo '<form name="TestSelec t_Form" method="GET" action="TestLis t.php"
                        enctype="text/plain" style="margin:0 px">
                                        <input type="radio" name="SelectOrN ot" value="Select_A ll"
                        onClick="CheckA ll(document.Tes tSelect_Form.Se lectModule)"
                        style="position :absolute;left: 290px;top:140px ;z-index:6">
                                        <input type="radio" name="SelectOrN ot" value="Uncheck_ All"
                        onClick="UnChec kAll(document.T estSelect_Form. SelectModule)" checked
                        style="position :absolute;left: 400px;top:140px ;z-index:7">';
                                $file_name = "CONF/testselect.txt" ;
                                $fp_TstSlct = fopen("$file_na me", "r") or die("Some error occurred
                        while opening 'testSelect.txt ");
                                $TopPos = 170;
                                while(!feof($fp _TstSlct))
                                {
                                        $TstSlct_data = fgets($fp_TstSl ct);
                                        preg_match_all( "/([#]*)(.*)\s(\d+)\s *[;#]*(.*)/", $TstSlct_data,
                        $match, PREG_PATTERN_OR DER);
                                        if ( $match[2][0] != NULL ) {   //Checking whether module is present
                        or not. May be only comments have been put. For. e.g.
                        ############### ####DSL HOME#########
                                                if ( $match[1][0] == NULL ) {   //Checking if line is starting with
                        '#' or not
                                                        echo '<input type="checkbox" name="SelectMod ule[]" value=' .
                        $match[2][0] . ' checked="checke d" style="position :absolute;left:
                        90px;top:' . $TopPos . 'px">';
                                                } else {
                                                        echo '<input type="checkbox" name="SelectMod ule[]" value=' .
                        $match[2][0] . ' style="position :absolute;left: 90px;top:' . $TopPos .
                        'px">';
                                                }
                        >
                                                echo '<div id="text3" style="position :absolute; overflow:hidden ;
                        left:130px; top:' . $TopPos . 'px; width:79px; height:16px"><d iv
                        class="wpmd">';
                                                echo '<div><font face="Arial">' . $match[2][0] . '</font></div>';
                                                echo '</div></div>';
                        >
                                                $TopPos = $TopPos + 25;
                                        }
                                }
                                fclose($fp_TstS lct);
                        >
                                echo '<input name="Configure TestList" type="submit" value="Next"
                        style="position :absolute;left: 326px;top:403px ;z-index:16">
                                </form>
                        >
                                <div id="text1" style="position :absolute; overflow:hidden ; left:
                        265px; top:118px; width:69px; height:18px; z-index:9"><div
                        class="wpmd">
                                <div><font class="ws12" face="Arial">Se lect All</font></div>
                                </div></div>
                        >
                                <div id="text2" style="position :absolute; overflow:hidden ; left:
                        371px; top:118px; width:85px; height:18px; z-index:10"><div
                        class="wpmd">
                                <div><font class="ws12" face="Arial">Un check All</font></div>
                                </div></div>';
                        ?>
                        >
                        Please someone suggest the solution as I'm totally stuck.
                        Thanks a lot in advance.
                        you can add a unique class name to all checkboxes and then use
                        getElementsByCl assName

                        example:

                        <script language="javas cript">
                        var checkAll = {
                        run:function(cl assName, checked){
                        var elements = document.getEle mentsByClassNam e(className);
                        for(i in elements){
                        elements[i].checked = checked;
                        }
                        }
                        }
                        </script>

                        <form ...>
                        <input type="checkbox" name="SelectMod ule[]" value="some value 1"
                        class="checkme anyOtherClass1" />
                        <input type="checkbox" name="SelectMod ule[]" value="some value 2"
                        class="checkme anyOtherClass2" />
                        <input type="checkbox" name="SelectMod ule[]" value="some value 3"
                        class="checkme anyOtherClass3" />
                        <input type="checkbox" name="SelectMod ule[]" value="some value 4"
                        class="checkme anyOtherClass4" />
                        </form>
                        <a href="javascrip t:checkAll.run( 'checkme',true) ">check</a>
                        <a href="javascrip t:checkAll.run( 'checkme',false )">uncheck</a>

                        Comment

                        • Richard Cornford

                          #13
                          Re: How to check all the checkboxes if checkbox name is 'name[]'

                          On Oct 29, 9:36 am, Suhas Dhoke wrote:
                          <snip>
                          I've modified your code, and here is the updated version..
                          >
                          #####
                          <script language="JavaS cript" type="text/javascript">
                          function checkAll (rad, selectType) {
                          for (var i=0;i < document.TestSe lect_Form.eleme nts.length;i++) {
                          var e = document.TestSe lect_Form.eleme nts[i];
                          if ('checkbox' == e.type && 'select' == selectType) {
                          Should the - 'select' == selectType - be inside the - for - loop and
                          repeatedly evaluated, given that it will always evaluate to the save
                          value?
                          e.checked = true;
                          } else {
                          This - else - branch is going to be entered whenever - 'checkbox' ==
                          e.type - is false, that does not seem like a good idea as it means
                          that all radio controls will have their - checked - properties set to
                          false.
                          e.checked = false;
                          }
                          }
                          rad.checked = true;}
                          <snip>^^^^^^^^^ ^^^^^^^^^^
                          Presumably this is here partly to mitigate for radio controls having
                          their - checked - properties set to false in the - else - branch?

                          Consider:-

                          function checkAll(rad, selectType){
                          var newValue = ('select' == selectType);
                          var el, c, els = rad.form.elemen ts;
                          if((c = els.length)){
                          do{
                          if(
                          (el = els[--c])&&
                          (el.type == 'checkbox')
                          ){
                          el.checked = newValue;
                          }
                          }while(c);
                          }
                          }

                          Richard.

                          Comment

                          • David Mark

                            #14
                            Re: How to check all the checkboxes if checkbox name is 'name[]'

                            On Oct 29, 5:39 am, khaled.jo...@gm ail.com wrote:

                            [snip]
                            >
                            you can add a unique class name to all checkboxes and then use
                            getElementsByCl assName
                            If you are developing exclusively for the very latest browsers, which
                            would not include the release versions of IE, then you can use that
                            method. However, it would not be an appropriate solution to this
                            problem.
                            >
                            example:
                            >
                            <script language="javas cript">
                            Don't use the language attribute. Use type="text/javascript".
                            var checkAll = {
                                run:function(cl assName, checked){
                            This is very odd.
                                    var elements = document.getEle mentsByClassNam e(className);
                            Blows up in most browsers.
                                    for(i in elements){
                            Filter for-in loops.
                                        elements[i].checked = checked;
                                    }
                                }}
                            >
                            </script>
                            >
                            <form ...>
                            <input type="checkbox" name="SelectMod ule[]" value="some value 1"
                            class="checkme anyOtherClass1" />
                            <input type="checkbox" name="SelectMod ule[]" value="some value 2"
                            class="checkme anyOtherClass2" />
                            <input type="checkbox" name="SelectMod ule[]" value="some value 3"
                            class="checkme anyOtherClass3" />
                            <input type="checkbox" name="SelectMod ule[]" value="some value 4"
                            class="checkme anyOtherClass4" />
                            </form>
                            <a href="javascrip t:checkAll.run( 'checkme',true) ">check</a>
                            <a href="javascrip t:checkAll.run( 'checkme',false )">uncheck</a>
                            Never use the javascript pseudo-protocol. Turn off scripting and see
                            that your document makes no sense.

                            Comment

                            Working...