0

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.

1
  • Am I right in thinking you want to turn [1,one,2,two,3,three] into {one: 1, two: 2, three: 3}? Commented Oct 27, 2010 at 10:30

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.