Check all checkboxes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • terence.parker@gmail.com

    #1

    Check all checkboxes

    I currently have the following JS in my header:

    function checkall(thesta te) {
    var checkboxes=eval ("document.form s.EssayList.fil e_id")
    for (i=0;i<checkbox es.length;i++)
    checkboxes[i].checked=thesta te
    }


    This works fine - but the problem is that it doesn't work with my
    checkboxes when they are set to name="file_id[]" . I have tried without
    the parenthesis, and that works fine - so I guess that's the problem.
    However I do need my POST to submit an array. (As a side question... is
    there even a point using checkboxes but not submitting an array? If you
    can only submit one item... why not use radio?) .

    Is there a way around my problem?

    Thanks,

    Terence

  • Richard Cornford

    #2
    Re: Check all checkboxes

    terence.parker@ gmail.com wrote:[color=blue]
    > I currently have the following JS in my header:
    >
    > function checkall(thesta te) {
    > var checkboxes=eval ("document.form s.EssayList.fil e_id")[/color]

    I nominate this line for this month's "Most futile use of eval" award.
    [color=blue]
    > for (i=0;i<checkbox es.length;i++)[/color]
    ^
    Using global variables as loop counters is an extremely bad habit to get
    into.
    [color=blue]
    > checkboxes[i].checked=thesta te
    > }
    >
    >
    > This works fine -[/color]

    More by coincidence than design given the - eval - use.
    [color=blue]
    > but the problem is that it doesn't work with my
    > checkboxes when they are set to name="file_id[]" .[/color]

    No it wouldn't. The item to the right of a dot in a dot notation
    property accessor must be an Identifier, and so only a limited set of
    character sequences may be used. The sequence file_id[] not only is not
    in that set of character sequences but you cannot place it, exposed, in
    javascript source code at all without producing a syntax error. Of
    course you can put (almost) any sequence of characters in a string
    literal without any consideration of its syntactic correctness as
    javascript source code (at least so long as you don't show it to the -
    eval - function). And because you can use "file_id[]" in a string, and
    can use strings as property names in bracket notation property
    accessors, you can avoid the problem of the element's name not being a
    valid javascript Identifier:-

    function checkAll(state) {
    var col = document.forms. EssayList.eleme tns['file_id[]'];
    for(var c = col.length;c--;){
    col[c].checked = state;
    }
    }

    - Though it would be better to perform the feature testing required to
    render the use of that function safe, at some point prior to the use of
    the function.

    Generally, see:-

    <URL: http://www.jibbering.com/faq/ >
    [color=blue]
    > I have tried without the parenthesis, and that
    > works fine - so I guess that's the problem.[/color]

    No need to guess, look at the error reports generated (though - eval -
    is a little inclined to mask errors).

    [color=blue]
    > However I do need my POST to submit an array. (As a side
    > question... is there even a point using checkboxes but not
    > submitting an array? If you can only submit one item...
    > why not use radio?) .[/color]

    You are misperceiving the relationship between an HTTP POST request and
    what your server-side technology (PHP?) does to interpret that request.
    HTTP POST requests have no concept of 'array'. All successful from
    controls have their names and values included in the HTTP POST request,
    so all checked checkboxes result in name/value pairs being sent. And
    multiple same-named checkboxes contribute multiple name/value pairs with
    the same name, and (theoretically, but not necessarily) different
    values. If your server side technology dose not expose that data to your
    code when it arrives then that is a limitation in the server-side
    technology (or, more likely, your understanding of it).

    Richard.


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Check all checkboxes

      "Richard Cornford" <Richard@litote s.demon.co.uk> writes:
      [color=blue]
      > terence.parker@ gmail.com wrote:[/color]
      [color=blue][color=green]
      >> This works fine -[/color][/color]
      [color=blue]
      > More by coincidence than design given the - eval - use.[/color]

      Cue:
      <URL:http://www.pragmaticpr ogrammer.com/ppbook/extracts/coincidence.htm l>

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • mscir

        #4
        Re: Check all checkboxes

        terence.parker@ gmail.com wrote:[color=blue]
        > I currently have the following JS in my header:
        >
        > function checkall(thesta te) {
        > var checkboxes=eval ("document.form s.EssayList.fil e_id")
        > for (i=0;i<checkbox es.length;i++)
        > checkboxes[i].checked=thesta te
        > }
        >
        >
        > This works fine - but the problem is that it doesn't work with my
        > checkboxes when they are set to name="file_id[]" . I have tried without
        > the parenthesis, and that works fine - so I guess that's the problem.
        > However I do need my POST to submit an array. (As a side question... is
        > there even a point using checkboxes but not submitting an array? If you
        > can only submit one item... why not use radio?) .
        >
        > Is there a way around my problem?
        > Thanks,
        > Terence[/color]



        How about checking all of the checkboxes in the form:

        function checkall(frm){
        for (var i=0; i<document.form s[frm].elements.lengt h; i++) {
        var e=document.form s[frm].elements[i];
        if ((e.type=='chec kbox') && (!e.disabled)){
        e.checked=true;
        }
        }
        }

        Mike

        Comment

        • terence.parker@gmail.com

          #5
          Re: Check all checkboxes

          Thanks for the reply - you solved my problem, so much appreciated. Just
          a few comments though:
          [color=blue][color=green]
          > > I currently have the following JS in my header:
          > >
          > > function checkall(thesta te) {
          > > var checkboxes=eval ("document.form s.EssayList.fil e_id")[/color]
          >
          > I nominate this line for this month's "Most futile use of eval" award.[/color]

          Thanks for the tip. However, as you correctly identified at the end of
          my post I was indeed using PHP - and since i'm learning the languages
          one at a time I know near to nothing about JS at the moment : this
          script was nabbed off javascriptkit.c om . In fact this is the second
          script I have taken from there, written about, then received replies
          saying how bad it was - so I guess now I know about the quality of the
          JS scripts from that site!
          [color=blue]
          > Using global variables as loop counters is an extremely bad habit to get
          > into.[/color]

          As with the above, I had no idea, but when I do come to learn JS i'll
          keep that tip in mind.
          [color=blue]
          > No it wouldn't. The item to the right of a dot in a dot notation
          > property accessor must be an Identifier, and so only a limited set of
          > character sequences may be used. -snip-
          >
          > function checkAll(state) {
          > var col = document.forms. EssayList.eleme nts['file_id[]'];
          > for(var c = col.length;c--;){
          > col[c].checked = state;
          > }
          > }[/color]

          Thanks for that - you did manage to solve my problem there so all is
          well now.

          [color=blue]
          > You are misperceiving the relationship between an HTTP POST request and
          > what your server-side technology (PHP?) does to interpret that request. -snip-[/color]

          Yup... you're right ; how silly of me, I wasn't thinking. As far as I
          gather though, PHP does require the form data to be submitted with the
          variable name as [] so that it enters an array when the PHP is
          executed. This is since even if I request a printout of the raw form
          POST it would only contain one field if I didn't use the parenthesis.

          What languages accept the form POST to be without the [] but yet retain
          the data when executed? Just curious.

          Thanks again,

          Terence

          Comment

          • askMe

            #6
            Re: Check all checkboxes

            I was thinking exactly the same thing.

            if ((e.type=='chec kbox') && (!e.disabled)){
            e.checked=true;
            }



            mscir wrote:[color=blue]
            > terence.parker@ gmail.com wrote:[color=green]
            > > I currently have the following JS in my header:
            > >
            > > function checkall(thesta te) {
            > > var checkboxes=eval ("document.form s.EssayList.fil e_id")
            > > for (i=0;i<checkbox es.length;i++)
            > > checkboxes[i].checked=thesta te
            > > }
            > >
            > >
            > > This works fine - but the problem is that it doesn't work with my
            > > checkboxes when they are set to name="file_id[]" . I have tried without
            > > the parenthesis, and that works fine - so I guess that's the problem.
            > > However I do need my POST to submit an array. (As a side question... is
            > > there even a point using checkboxes but not submitting an array? If you
            > > can only submit one item... why not use radio?) .
            > >
            > > Is there a way around my problem?
            > > Thanks,
            > > Terence[/color]
            >
            > http://forums.devarticles.com/archiv...t-all-checkbox
            >
            > How about checking all of the checkboxes in the form:
            >
            > function checkall(frm){
            > for (var i=0; i<document.form s[frm].elements.lengt h; i++) {
            > var e=document.form s[frm].elements[i];
            > if ((e.type=='chec kbox') && (!e.disabled)){
            > e.checked=true;
            > }
            > }
            > }
            >
            > Mike[/color]

            Comment

            • Richard Cornford

              #7
              Re: Check all checkboxes

              terence.parker@ gmail.com wrote:
              <snip>[color=blue]
              > What languages accept the form POST to be without
              > the [] but yet retain the data when executed?
              > Just curious.[/color]

              In Java the - getParameterVal ues - method of the - ServletRequest -
              interface returns an array of all of the values from same-name
              name/value pairs in an HTTP request without any restriction on the names
              used.

              Richard.


              Comment

              Working...