Cycle Through Multiple Checkboxes.

Discussion in 'Javascript' started by @(none), Jan 7, 2005.

  1. @(none)

    @(none) Guest

    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.Select.CheckBox" +n +".checked"
    if ( s ) {
    alert ( s )
    }
    alert ("Done")


    If I use "document.Select.CheckBox0.checked" 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.
     
    @(none), Jan 7, 2005
    #1
    1. Advertising

  2. @(none)

    Lee Guest

    none said:
    >
    >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.Select.CheckBox" +n +".checked"
    > if ( s ) {
    > alert ( s )
    > }
    > alert ("Done")



    Use the array notation:

    for (var i=0; i<num;i++) {
    if(document.Select.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=document.Select.elements;
    if(element.type=="checkbox" && element.checked) {
    alert(element.name+" 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.Select.CheckBox.checked) {
    alert("CheckBox["+i+"] is checked");
    }
    }

    None of this is tested, beware of typoes.
     
    Lee, Jan 7, 2005
    #2
    1. Advertising

  3. @(none)

    sunami Guest

    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.length; i++)
    {
    if (checkboxes.checked)
    {
    // do something
    }
    }
     
    sunami, Jan 7, 2005
    #3
  4. @(none)

    Mick White Guest

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


    for(i=0;i<num;i++){
    s=document.Select["CheckBox" +n].checked;
    if(s) alert(s);
    }
    alert ("Done");
    Mick
     
    Mick White, Jan 8, 2005
    #4
  5. On 7 Jan 2005 15:51:28 -0800, sunami <> wrote:

    Please quote relevant text (delete the rest) from the previous post when
    replying.

    > you can put "s" in an eval statement ...
    >
    > if (eval(s))
    > ...


    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.
     
    Michael Winter, Jan 8, 2005
    #5
  6. @(none)

    @(none) Guest

    Mick White wrote:
    > none wrote:
    > [...]
    >
    >>
    >> for ( i=0; i<num; i++ ) {
    >> n = i.toString()
    >> s = "document.Select.CheckBox" +n +".checked"
    >> if ( s ) {
    >> alert ( s )
    >> }
    >> alert ("Done")
    >>
    >>

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



    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.Select["CheckBox" +n].checked;

    Works perfectly - thanks for all the help ;-)
     
    @(none), Jan 8, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mark
    Replies:
    2
    Views:
    290
  2. Tim Zych
    Replies:
    2
    Views:
    182
    Tim Zych
    Nov 28, 2005
  3. Tom Ha
    Replies:
    4
    Views:
    114
    Tom Ha
    Dec 17, 2008
  4. Marc
    Replies:
    3
    Views:
    99
  5. Replies:
    18
    Views:
    275
    Thomas Kratz
    Mar 21, 2006
Loading...

Share This Page