0

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.

3
  • "this isn't working": please elaborate Commented Feb 18, 2012 at 0:38
  • source: url works but source: data doesn't Commented Feb 18, 2012 at 0:38
  • 2
    remove the $(document).ready() wrapper function. Commented Feb 18, 2012 at 0:41

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

This method doesn't have much real world use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.