[Javascript] request for assistance... -- associative arrays

Paul Novitski paul at novitskisoftware.com
Sat Apr 10 02:59:43 CDT 2004


Walter,

Here (attached) is my solution to your problem.  Each time you encounter a 
new Family, add it as a new element to the aryCkFamily() array and 
initialize it as a new sub-array, then add each input field id/value pair 
you encounter to its Family sub-array.

I changed the code where it checks a form element, adding textareas, and 
separating the criteria-check from the rest of the code simply to keep the 
indentation in hand...

I also added a debug display loop at the end to check the logic and to 
demonstrate how to walk through the two-dimensional associative array after 
building it.

Cheers,
Paul



At 03:41 PM 4/9/2004, jsWalter wrote:
>I'm a bit stumped.
>
>I need to dynamically create an assoc array
>
>Below is the code I have so far.
>
>What I'm looking to do is loop through all FORM elements, pull out the
>INPUT->CHECKBOX types and store them in an assoc array by FAMILY [custom]
>attribute value
>
>  - new FAMILY name, new key
>  - same FAMILY name, new item to key
>
>Anyone have a quick answer?
>
>Thanks
>
>Walter
>
>
>======================================================================
>
><label for="cb1a"><input type="checkbox" family="rb1" name="cb1" id="cb1a"
>value="a"> a</label>
><label for="cb1b"><input type="checkbox" family="rb1" name="cb1" id="cb1b"
>value="b"> b</label>
><p />
>
><label for="cb2a"><input type="checkbox" family="rb2" name="cb2" id="cb2a"
>value="1"> 1</label>
><label for="cb2b"><input type="checkbox" family="rb2" name="cb2" id="cb2b"
>value="2"> 2</label>
><p />
>
><label for="zz"><input type="checkbox" name="zz" id="zz" value="z">
>z</label>
></form>
>
><script type="text/javascript">
>
>// get the first FORM of this document
>var form = document.forms[0];
>
>// create new Array
>var aryCkFamily = new Array();
>
>// initialize Object counter
>var y = 0;
>
>// loop through all FORM Objects
>for (var i = 0; i < form.length ; i++)
>{
>         // We are looking for INPUT Objects with
>         // the TYPE of CHECKBOX.
>         // We are also looking for TEXTAREA Objects
>         if ( ( form.elements[i].nodeName.toLowerCase() == "input" ) &&
>                                 ( form.elements[i].type.toLowerCase() == 
> "checkbox" ) )
>         {
>                 // We only this CHECKBOX if it has out FAMILY attribute
>                 if ( form.elements[i].getAttribute('family') )
>                 {
>                         // NOTE: Here...
>                         // need code to add new INPUT CHECKBOX objects into
>                         // associative array by FAMILY attribute value
>                         // - new FAMILY name, new key
>                         // - same FAMILY name, new item to key
>                 }
>         }
>}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20040410/8d6294dd/attachment.html>


More information about the Javascript mailing list