Results 1 to 8 of 8

Thread: javascript for loop....

  1. #1
    Join Date
    Sep 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default javascript for loop....

    hey was just wondering...if i have like a set of checkboxes or something like...

    <input type="checkbox" name="checkbox1"/>
    <input type="checkbox" name="checkbox2"/>
    <input type="checkbox" name="checkbox3"/>
    <input type="checkbox" name="checkbox4"/>
    <input type="checkbox" name="checkbox5"/>

    how would i write a function to check if the check boxes are checked? like..

    function checked()
    {
    for (i=1; i<= 5; i++)
    document.getElementByID("form").....
    }

    or something like that....
    any suggestions?

    i know that i can just check each checkbox individually but the checkboxes are created dynamically using an external array and the arrays are of different length so yea...

    i need a loop cause theres some values assigned and i need to make a total... like if checkbox is ticked total increases (kind of like a count)

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    You're very close, and the idea should work, but it needs a few tweaks.
    -Put the inputs in a form
    -Give the form the "name" attribute
    -Apply it in the script below:
    Code:
    function checked()
    {
    for (i=1; i<= 5; i++)
    if (document.forms["myFormName"].elements["checkbox"+i].checked) {
      //The current box in the loop IS checked, do something here.
      alert("Checkbox "+i+" is checked.");
      }
    }
    - Mike

  3. #3
    Join Date
    Sep 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanx heaps dude
    yea i already have the form tags and stuff
    just needed the function code cause i wan't sure how to the the loop and yea
    thax heaps!!

  4. #4
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Quote Originally Posted by mburt View Post
    You're very close, and the idea should work, but it needs a few tweaks.
    -Put the inputs in a form
    -Give the form the "name" attribute
    -Apply it in the script below:
    Code:
    function checked()
    {
    for (i=1; i<= 5; i++)
    if (document.forms["myFormName"].elements["checkbox"+i].checked) {
      //The current box in the loop IS checked, do something here.
      alert("Checkbox "+i+" is checked.");
      }
    }
    Don't you have to declare "i" first?
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by tech_support View Post
    Don't you have to declare "i" first?
    No, but you should or should declare it formally in the loop. Otherwise, it is part of the global scope and can easily be messed with by other scripts or mess with another global i in another script.

    This is good enough:

    Code:
    for (var i=1; i<= 5; i++)
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    That's what I meant
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's kind of inefficient, because you're obtaining a reference to the form again every time the loop iterates. This is one of the most common causes of slowness in scripts. Better would be to get a single reference and store it:
    Code:
    function checked() {
      var f = document.forms["myFormName"].elements;
      for (var i = 0, f = document.forms['myFormName'].elements, e; e = f[i]; ++i)
        if (e.checked) {
          // The current box in the loop IS checked, do something here.
          alert("Checkbox " + i + " is checked.");
        }
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Don't you have to declare "i" first?
    I was simply adding to the code the OP already had there. I know that you have to use the var keyword.
    - Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •