Skip to content

How to use autocomplete widget

Chandan Kharbanda edited this page Apr 3, 2015 · 2 revisions

How to use autocomplete widget?

    $(input).autocomplete( url, displayName, idElem, idKey );

url

URL returning all the completion object in array of object form in JSON.

For example,

    [{id: 1, val: 'hello'}, {id: 2, val: 'hey'}]

displayKey

Which key to display and search in for autocomplete.

In our example, 'val'

idElem (optional)

If you want to submit or set id or some other field of selected element in some other input, pass the DOM element of other input here.

idKey (optional)

If you set idElem, pass key for the value to be set in idElem

Example

    <input id='greet_id' name='greet_id'>
    <input id='greet' name='greet'>
    $('#greet').autocomplete(
        url,            // URL returning out example JSON
        'name',         // field to display and search
        $('#greet_id'), // Element to set id in
        'id'            // Field for id
    );