Control Freak
Control Freak

Reputation: 13233

Using getJSON data for autocomplete source

Trying to first .getJSON then using that data to become the source of my autocomplete, heres the code.. this isn't working, what am i doing wrong here?

          $.getJSON(url, function(data) { 
                  //autocomplete
                 $(document).ready(function(){
                     $( "#name" ).autocomplete({
                          minLength: 2,
                          source: data
                      })
                  });             
           });

I know i can do source: url but i don't want to make multiple calls to the jSON data.

Upvotes: 0

Views: 5606

Answers (1)

Ricardo Souza
Ricardo Souza

Reputation: 16456

You have to inverse the document ready event handler to wrap the $.getJSON aswell:

$(document).ready(function(){
    $.getJSON(url, function(data) { 
         //autocomplete           
         $( "#name" ).autocomplete({
             minLength: 2,
             source: data
          })
    });             
});

Also, your data has to be an array. If its JSON, see this for reference: jquery autocomplete with json response

Upvotes: 6

Related Questions