Cycle Through Multiple Checkboxes.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • @(none)

    Cycle Through Multiple Checkboxes.

    I have a page which is a set of CheckBoxes generated daily and thus the
    number of Checkboxes changes each day.

    What I want to do is allow the user to select one or more checkboxes and
    the push a "Done" button and then have a script which uses a "for" loop
    to check the status of each box. The code I use for this is

    Sample checkbox HTML

    <P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox0 "
    VALUE="AFL"> &quot;AFL, Score = -2&quot;
    <P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox1 "
    VALUE="ALL"> &quot;ALL, Score = 1&quot;
    <P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox2 "
    VALUE="ANN"> &quot;ANN, Score = 6.15&quot;
    <P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox3 "
    VALUE="BBA"> &quot;BBA, Score = 3&quot;

    code to check the State of each checkbox

    for ( i=0; i<num; i++ ) {
    n = i.toString()
    s = "document.Selec t.CheckBox" +n +".checked"
    if ( s ) {
    alert ( s )
    }
    alert ("Done")


    If I use "document.Selec t.CheckBox0.che cked" in the "if" it works OK.
    The problem is building the "if" parameter in the for loop and maybe I
    am missing the point about type conversion - although I have googled for
    info and ther doesn't appear to be a type conversion. I dont think I can
    use a string in the "if" - so how do I build a type that "if" is happy
    with ??

    Hope someone can help - Thanks.
  • sunami

    #2
    Re: Cycle Through Multiple Checkboxes.

    you can put "s" in an eval statement ...

    if (eval(s))
    ....


    if you can give each checkbox the same name, you can reference it as an
    array and loop over it ...

    checkboxes = document.forms[0].nameOfCheckbox ;

    for (i=0; i < checkboxes.leng th; i++)
    {
    if (checkboxes[i].checked)
    {
    // do something
    }
    }

    Comment

    • Lee

      #3
      Re: Cycle Through Multiple Checkboxes.

      none said:[color=blue]
      >
      >I have a page which is a set of CheckBoxes generated daily and thus the
      >number of Checkboxes changes each day.
      >
      >What I want to do is allow the user to select one or more checkboxes and
      >the push a "Done" button and then have a script which uses a "for" loop
      >to check the status of each box. The code I use for this is
      >
      >Sample checkbox HTML
      >
      ><P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox0 "
      >VALUE="AFL"> &quot;AFL, Score = -2&quot;
      ><P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox1 "
      >VALUE="ALL"> &quot;ALL, Score = 1&quot;
      ><P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox2 "
      >VALUE="ANN"> &quot;ANN, Score = 6.15&quot;
      ><P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox3 "
      >VALUE="BBA"> &quot;BBA, Score = 3&quot;
      >
      >code to check the State of each checkbox
      >
      > for ( i=0; i<num; i++ ) {
      > n = i.toString()
      > s = "document.Selec t.CheckBox" +n +".checked"
      > if ( s ) {
      > alert ( s )
      > }
      > alert ("Done")[/color]


      Use the array notation:

      for (var i=0; i<num;i++) {
      if(document.Sel ect.elements["CheckBox"+ i].checked) {
      alert("CheckBox "+i+" is checked");
      }
      }

      or, if there are no other checkboxes in the form, you can just loop through the
      form looking at all of the checkboxes, disregarding the names:

      for (var i=0;i<document. Select.elements .length;i++) {
      var element=documen t.Select.elemen ts[i];
      if(element.type =="checkbox" && element.checked ) {
      alert(element.n ame+" is checked");
      }
      }

      or, if you're not submitting the form to a script that cares about the names,
      you could rename all of the checkboxes to "CheckBox", which makes them available
      as an array named CheckBox:

      for (var i=0;i<document. Select.CheckBox .length;i++) {
      if (document.Selec t.CheckBox[i].checked) {
      alert("CheckBox["+i+"] is checked");
      }
      }

      None of this is tested, beware of typoes.

      Comment

      • Mick White

        #4
        Re: Cycle Through Multiple Checkboxes.

        none wrote:
        [...][color=blue]
        >
        > for ( i=0; i<num; i++ ) {
        > n = i.toString()
        > s = "document.Selec t.CheckBox" +n +".checked"
        > if ( s ) {
        > alert ( s )
        > }
        > alert ("Done")
        >
        >[/color]

        for(i=0;i<num;i ++){
        s=document.Sele ct["CheckBox" +n].checked;
        if(s) alert(s);
        }
        alert ("Done");
        Mick

        Comment

        • Michael Winter

          #5
          Re: Cycle Through Multiple Checkboxes.

          On 7 Jan 2005 15:51:28 -0800, sunami <thuong@gmail.c om> wrote:

          Please quote relevant text (delete the rest) from the previous post when
          replying.
          [color=blue]
          > you can put "s" in an eval statement ...
          >
          > if (eval(s))
          > ...[/color]

          The eval function does have it's uses, but this is not one of them.

          /* Declare variables that should have local scope
          * with the var keyword. In fact, it's good form
          * to declare *all* variables with var.
          */
          var elements = document.forms['Select'].elements;
          for(var i = 0; i < num; ++i) {
          if(elements['CheckBox' + i].checked) {
          /* ... */
          }
          }

          [snip]

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          • @(none)

            #6
            Re: Cycle Through Multiple Checkboxes.

            Mick White wrote:[color=blue]
            > none wrote:
            > [...]
            >[color=green]
            >>
            >> for ( i=0; i<num; i++ ) {
            >> n = i.toString()
            >> s = "document.Selec t.CheckBox" +n +".checked"
            >> if ( s ) {
            >> alert ( s )
            >> }
            >> alert ("Done")
            >>
            >>[/color]
            >
            > for(i=0;i<num;i ++){
            > s=document.Sele ct["CheckBox" +n].checked;
            > if(s) alert(s);
            > }
            > alert ("Done");
            > Mick[/color]


            Hey !! That was really great - the usual story - write 400 lines of code
            ( some of it really tricky ) and end up getting screwed for a day and a
            half trying to test the status of a bloody checkbox.

            The line
            s=document.Sele ct["CheckBox" +n].checked;

            Works perfectly - thanks for all the help ;-)

            Comment

            Working...