views:

25

answers:

1

I am using this jquery ui autocomplete but i need to grab the data from a json object every keystroke and put it in the dropdown as the user types. But the example uses just an array

<script type="text/javascript">
$(function() {
    var availableTags = ["ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme"];
    $("#request_artist").autocomplete({
        source: availableTags
    });
});
</script>

I was thinking of doing the ajax request on the keypress but that completely contradicts the autocomplete functionality

+3  A: 

There's an example on the plug-in page at http://jqueryui.com/demos/autocomplete/#remote-jsonp Just click the View Source link. The relevant part is:

    $("#city").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "http://ws.geonames.org/searchJSON",
                dataType: "jsonp",
                data: {
                    featureClass: "P",
                    style: "full",
                    maxRows: 12,
                    name_startsWith: request.term
                },
                success: function(data) {
                    response($.map(data.geonames, function(item) {
                        return {
                            label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                            value: item.name
                        }
                    }))
                }
            })
        },
        minLength: 2,
        select: function(event, ui) {
            log(ui.item ? ("Selected: " + ui.item.label) : "Nothing selected, input was " + this.value);
        },
        open: function() {
            $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function() {
            $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    });
Dan Diplo
you beat me to it... +1
moi_meme