#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    29
    Rep Power
    0

    Javascript problem


    Hi!

    I want to get the status (true or false) of a checkbox with Javascript. Normally, I use "document.myform.mycheckbox.checked", but in this case, "mycheckbox" is an array.

    "document.myform.mycheckbox[0].checked" doesn't work.
    "document.myform.mycheckbox[].checked" doesn't work.
    "document.myform.mycheckbox.0.checked" doesn't work...

    Who knows the answer?

    Thanks!
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2001
    Location
    England
    Posts
    967
    Rep Power
    14
    Something like this?

    function ValidateCheckBoxes() {

    var checkered_flag = 0;

    for (var i = 0; i < 10; i++) {
    if (document.myform.elements["checkboxname[" + i + "]"].checked == true) {
    checkered_flag++;
    }
    }

    if (checkered_flag == 0) {
    return false;
    }
    return true;
    }

    would loop through an array of checkboxes called "checkboxname[0]" to "checkboxname[9]" in a form called "myform" - if one or more of the checkboxes is checked, the function returns true. Be more specific if this wasn't what you wanted the function to do...
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    29
    Rep Power
    0
    I didn't need the function you described, but you helped me great with this part of your reply:

    document.myform.elements["checkboxname[" + i + "]"].checked
    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo