YMELS
YMELS

Reputation: 153

Add object array to jquery-ui autocomplete

I'm using jquery-ui plugin, what I'm trying to accomplish is to use an array of pair values (Key and label value) as the source for an autocomplete text input. My array looks like:

var valuesArray = [{
    label: 'Name_1',
    key: 1
}, {
    label: 'Name_2',
    key: 2
}, {
    label: 'Name_3',
    key: 3
}, {
    label: 'Name_4',
    key: 4
}];

I need the label attribute to appear in the text input while getting the key attribute when retrieving the input text element value.

Thanks in advance

Upvotes: 0

Views: 1623

Answers (1)

cubanGuy
cubanGuy

Reputation: 1296

I think that from what i understand, this is what you need.

    $("#someID").autocomplete({        
    source: function(request, response) {
        var data = [{
        label: 'Name_1',
        key: 1
    }, {
        label: 'Name_2',
        key: 2
    }, {
        label: 'Name_3',
        key: 3
    }, {
        label: 'Name_4',
        key: 4
    }];

        response(data);
    },
    select: function( event, ui ) {
        $( "#someID" ).val( ui.item.key); 
        alert(ui.item.key);
        return false;
    }
});

Upvotes: 1

Related Questions