CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2002
    Location
    PA
    Posts
    36

    multiple checkbox question

    hey there...

    i'm using IE 6...and i've got this javascript code:

    var totalCheck = theForm.chkDelete.length;

    alert(theForm.chkDelete.value);
    alert(theForm.chkDelete.length);

    okay...now i have this html on the page:

    <input type="checkbox" name="chkDelete" value="#EventID#">

    instead a cfoutput loop...so in a loop.

    now when i have 2 or more checkboxes on the page it works fine...i get

    chkDelete.value = (whatever the first EventID is)
    chkDelete.length = 2

    but when i have only one checkbox the .value works the same...but the length is undefined.

    what's going on here and how can i test for just one box if it won't work this way?

    thanks!
    b

  2. #2
    Join Date
    Aug 2002
    Location
    Reykjavik, Iceland
    Posts
    201
    I'm assuming all of your checkboxes have the same name??

    If so, they are referred to as indices in an array - chkDelete[0], chkDelete[1], etc.

    The length property defines how many members are in the array.
    When there is only one checkbox, the length is undefined because there have to be at least 2 members in any array in JavaScript.

    For the special case of one checkbox, simply do:

    if (theForm.chkDelete.length) {
    // code for 2 or more checkboxes
    for (i=0; i< theForm.chkDelete.length; i++) {
    foo = theForm.chkDelete[i].value;
    alert(foo);
    }
    } else {
    // code for 1 checkbox
    foo = theForm.chkDelete.value;
    alert(foo);
    }

  3. #3
    Join Date
    Aug 2002
    Location
    PA
    Posts
    36
    this is what i was thinking you'd say...i just couldn't find any documentation on it for sure. sounds like a good fix.

    i should've know the websmith would know...again. thanks!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured