Results 1 to 4 of 4

Thread: [Resolved] Form Object arrays in Javascript

  1. #1

    Thread Starter
    Addicted Member MasterBlaster's Avatar
    Join Date
    Jul 2002
    Location
    Seattle
    Posts
    196

    [Resolved] Form Object arrays in Javascript

    I have a bunch of checkboxes on a page. they are added to the form using server script. the final source looks like

    Code:
    <input id="Documents2_Repeater1__ctl3_0" type="checkbox" value="3996a1f6-7e71-4dbe-b104-6f1abb7a0dfa" />
    
    <input id="Documents2_Repeater1__ctl3_1" type="checkbox" value="3996a1f6-7e71-4dbe-b104-6f1abb7a0dfa" />
    
    <input id="Documents2_Repeater1__ctl3_2" type="checkbox" value="3996a1f6-7e71-4dbe-b104-6f1abb7a0dfa" />
    Is there any way using client Javascript to loop through these checkboxes and add the value field to an array list? I do not know how many checkboxes there will be. It depends on the database.
    Last edited by MasterBlaster; Feb 22nd, 2005 at 03:07 PM.
    "And most of the evils of society can, in fact, be cured through information. We have a society that has been disinformed and based on the disinformation has made irrational choices. And that's what I mean by 'ignorance.' People, who ordinarily might be smart, are deprived of the data by which to make a rational decision, don't have the data to do it."
    Frank Zappa

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Form Object arrays in Javascript

    You firstly need to give them all identical names, i.e:
    Code:
    <input id="Documents2_Repeater1__ctl3_0" type="checkbox" value="3996a1f6-7e71-4dbe-b104-6f1abb7a0dfa" name="test"/>
    
    <input id="Documents2_Repeater1__ctl3_1" type="checkbox" value="3996a1f6-7e71-4dbe-b104-6f1abb7a0dfa" name="test"/>
    
    <input id="Documents2_Repeater1__ctl3_2" type="checkbox" value="3996a1f6-7e71-4dbe-b104-6f1abb7a0dfa" name="test"/>
    Then you can access it as an array of the parent form and loop through it like you would any other array:
    HTML Code:
    <script type="text/javascript">
    <!--
    var theForm = document.formname;
    var count = theForm.test.length;
    
    for (var i = 0; i < count; i++)
    {
        alert(theForm.test[i].value);
    }
    //-->
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090

    Re: Form Object arrays in Javascript

    Or, you don't have to bother with the names and use a while loop instead:
    HTML Code:
    <script type="text/javascript">
    <!--
    i=0
    MyArray = new Array()
    while (document.getElementById('Documents2_Repeater1__ctl3_'+i))
    {
        MyArray[i] = document.getElementById('Documents2_Repeater1__ctl3_'+i).value
        i++
    }
    -->
    </script>
    Have I helped you? Please Rate my posts.

  4. #4

    Thread Starter
    Addicted Member MasterBlaster's Avatar
    Join Date
    Jul 2002
    Location
    Seattle
    Posts
    196

    [Resolved] Form Object arrays in Javascript

    Awesome! Thanks!
    "And most of the evils of society can, in fact, be cured through information. We have a society that has been disinformed and based on the disinformation has made irrational choices. And that's what I mean by 'ignorance.' People, who ordinarily might be smart, are deprived of the data by which to make a rational decision, don't have the data to do it."
    Frank Zappa

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