java_dev
java_dev

Reputation: 125

how can I get the "ID" of selected value in Jquery Ui Autocomplete

i am using JqueryUI autocomplete . after source method i written select method. in that ui.item.value showing the result what i selected value, but ui.item.id is not working it showing 'undefined' . how can i get seleted value ID? please find my code here http://jsfiddle.net/hari034/R8xGA/

sample code URL returns:

3045:MUM:MUMBAI^

Description:

3045 : placeID

MUM : Place Code

MUMBAI : place Name.

^ : all placed coming from DB separated by "^"

Upvotes: 1

Views: 197

Answers (2)

Andrew Whitaker
Andrew Whitaker

Reputation: 126042

ui.item.id is undefined because you're not adding it to each item in autocomplete's source. You can accomplish this by tweaking your succeess function in your AJAX call:

success: function(data) {

    fromPlacesNamesList = new Array();
    var f = data.split("^");
    for (indx = 0; indx < f.length - 1; indx++) {
        var f1 = f[indx].split(':');
        fromPlacesNamesList[indx] = { label: f1[2], id: f1[0] };
    }
    response(fromPlacesNamesList);
    return false;
} //success

Additionally, you should have been calling response from outside of your for loop.

Updated example: http://jsfiddle.net/7GBTH/

Upvotes: 2

Marek Tuchalski
Marek Tuchalski

Reputation: 489

I've prepared some kind of resolution for your problem. There is no direct way to get information from autocomplete (or at least I don't know it). Please find working example here: http://jsfiddle.net/R8xGA/3/

Basically you need to save results ids in global array which is later on accessed in select.

Hope it helps!

EDIT: You basically need to add 3 elements

  1. Before for(indx=0;indx<f.length-1;indx++){ add array reset ids = new Array;
  2. In for statement declare array with ids with value as a key ids[f1[2]] = f1[0];
  3. Get the information from your array in select phase ids[ui.item.value]

PS. This trick will not work if you have duplicated values. It will be tough to add support for such use case.

Best, Marek

Upvotes: 0

Related Questions