views:

55

answers:

2

I'm trying to build a javascript that will autocomplete a textfield based off of a customer name in an array, but I would also like to set a hidden field to the customer ID. Im not sure how to build an associative array that will allow me to accomplish this. I've found a bunch of sniplets for autocomplete, but I'm struggling on how to build the array and subsequently referencing it to set 2 html tags:

<SCRIPT language="JavaScript">
function autocomplete(filter)
{
var filter = 1,one,2,two,3,three
}
</SCRIPT>
    <input type='hidden' id='id' />
    <input type='text' id='custname' -onKeypress='autocomplete();' />

Edit: The function I am calling has a single array passed to it to perform the autocomple. So If I can figure out how to build a new array from the the Evens and another from the Odds in the filter variable, I should be okay.

A: 
var customers = {}; 
customers["1"] = "Name 1"; 
customers["2"] = "Name 2"; 
customers["3"] = "name 3";

    // iterating
for (i in customers) {
    alert (i);      // key
    alert (customers[i]);   // value
}

You can find detailed descriptions and examples on the following page: Associative arrays

gumape
+1  A: 

Assuming that you want to turn [1,one,2,two,3,three] into {one: 1, two: 2, three: 3}:

function makeAssociative(array)
{
    var associative;
    if(array.length % 2 == 0)
    {
        associative = {};
        for(int i = 0; i<array.length; i += 2)
        {
            associative[i+1] = associative[i];
        }
    }

    return associative;
}
Eric