Blog Archive

jQuery autocomplete force selection - display list on focus using minLength = 0

How to create a dropdown in jQuery using autocomplete and force selection from list

In this example we created a drop down <SELECT> list by setting the minLength config to 0 and then check for the focus event to trigger a search that brings all the values for the autocomplete. In addition to that when you move away from the input field it compares the input text value to the list data for a match, otherwise just blanks out the field.

There is no force selection config currently available in jQuery autocomplete feature.

dropdown in jQuery using autocomplete and force selection
dropdown in jQuery using autocomplete and force selection

Source for the HTML page - index.html

    <title>jQuery Drop Down using AutoComplete and minLength of 0</title>

    <link href="" rel="stylesheet" type="text/css" />
    <STYLE TYPE="text/css" media="all">
        .ui-autocomplete { 
            position: absolute; 
            cursor: default; 
            height: 150px; 
            overflow-y: scroll; 
            overflow-x: hidden;
    <script src="" type="text/javascript"></script>
    <script src="" type="text/javascript"></script>
    <script type="text/javascript" src="app.js"></script>


    <form id="jQueryAutocomplete">
            <legend>jQuery create Drop down using autocomplete</legend>

                    <label for="myDropDown">Just click to see:</label><br />
                    <input id="myDropDown" type="text" name="myDropDown" />


Source for the JavaScript file using jQuery - app.js

$(document).ready(function() {

    var source = ['jQuery', 'Dojo', 'ExtJs', 'Prototype', 'Java', 'Android', 'MySQL', 'PHP'];
        minLength: 0,
        source: source,
        autoFocus: true,
        scroll: true,
    }).focus(function() {
        $(this).autocomplete("search", "");
    }).live("blur", function(event) {
        var autocomplete = $(this).data("autocomplete");
        var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i");
        autocomplete.widget().children(".ui-menu-item").each(function() {
            //Check if each autocomplete item is a case-insensitive match on the input
            var item = $(this).data("item.autocomplete");
            if (matcher.test(item.label || item.value || item)) {
                //There was a match, lets stop checking
                autocomplete.selectedItem = item;
        //if there was a match trigger the select event on that match
        if (autocomplete.selectedItem) {
            autocomplete._trigger("select", event, {
                item: autocomplete.selectedItem
        //there was no match, clear the input
        } else {