SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member GWHicks's Avatar
    Join Date
    Oct 2004
    Location
    Hemet, CA
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrays and checkboxes

    OK, I have been working on this for the last couple days and have made a lot of headway, but am still stuck on a spot. I have a group of checkboxes on my form that allow the user to select which items apply. I want to verify that at least one checkbox in each group has been selected before allowing them to continue. PHP requires the naming of the arrays to include the [] but jsp (for verification) doesn't like it. I read about and used the .elements method to overcome part of that problem so my for loop is now executing for the number of items in my group:

    for (var i=0; i<document.form1.elements['dining_out1[]'].length; i++)

    however my if statement won't execute:

    if (document.form1.elements['dining_out1[i]'].checked)

    even if I have checked ALL checkboxes. I have tried all the different methods I can think of to test for a checked value on each item in the array, but I can't quite figure out what I am missing.

    Any help will be greatly appreciated.

    Greg

    P.S. I had posted this to the Java and JSP forum and then realized it should actually be here so I am reposting it, and if possible I will now go delete my other thread. Sorry for the crosspost... been a longtime lurker, just now feel like I am getting into the code deep enough to ask some decent questions!
    -------------------------------------------------
    Greg Hicks
    Hicks Computer Consulting
    -------------------------------------------------

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.form1.elements['dining_out1'][i].checked

  3. #3
    SitePoint Member GWHicks's Avatar
    Join Date
    Oct 2004
    Location
    Hemet, CA
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tried that and it didn't work (but as a disclaimer I tried using Firefox and not IE which I have seen handle this whole situation completely differently. I ended up finding an answer in another forum post (I know I should have looked deeper before posting, my bad).

    Here is the solution that got me working:

    var dining_array = document.getElementsByName('dining_out1[]');

    for (var i=0; i<dining_array.length; i++)
    {
    if (dining_array[i].checked)
    {
    test_dining = 1;
    alert(dining_array[i].value + " was checked")
    }
    }

    The getElementsByName allowed me to work with the array again and seems to have passed the proper values over (I don't need the actual values, just to verify that at least one checkbox was checked in each group) but it is still nice to see that I could use this for further data handling if necessary.

    Greg
    -------------------------------------------------
    Greg Hicks
    Hicks Computer Consulting
    -------------------------------------------------


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
  •