views:

79

answers:

3

How do I get the value of the index position in the array, whose element the user had chosen using the autocomplete?

For e.g. if I enter the array having two elements as the input for the autocomplete plugin:

var arr = [];
arr[0] = "John";
arr[1] = "Paul";

Then, say user selects "Paul", how do I get the value of the selected index "1"?

A: 

As I understand your question, you could just do something like

function FindIndex( arr, searchValue ){
    for( var i = 0; i < arr.length; ++i ){
        if( arr[i] === searchValue ){
            return i;
        }
     }
 }
BioBuckyBall
Can also do that with the jQuery find() method. http://docs.jquery.com/Traversing/find#expr
Cheeso
A: 

This ref http://docs.jquery.com/Attributes/val#val says you can use a selector like so:

$('#selection option:selected')....
Cheeso
A: 
var arr = [];
arr[0] = "John";
arr[1] = "Paul";
...
//user selects something
//assuming select value now is in an input field
var index = jQuery.inArray($("input#yourselector").val(), arr);
if (index > -1)
    alert(index);
else
    alert("value not found in autocomplete array");
jitter