19

I'm using version 1.8.13 of jQuery UI's Auto complete and by default jQuery is using the query parameter of "?term=" by default, while my app is using "?q=" in the string it is creating. I COULD change the variable to be "term" on the backend, but I'd rather just tell jQuery what the server is trying to send it. Is there a way to change this?

Right now I just have something like this and it works if I change the variable to "term" on the backend, but like I said I wanted to change it to "q" and I can't find any info online about setting the parameter (that works):

$( "#input-search").autocomplete({
   source: "/search/autocomplete/"
});

2 Answers 2

28

You could use the callback form of source and handle all the interaction with the server yourself. Something like this:

$("#input-search").autocomplete({
    source: function(request, response) {
        $.get('/search/autocomplete', { q: request.term }, function(data) {
            response(data.split('\n'));
        });
    }
});
1
  • worth noting that you can actually change the search term using source too (I clean up email addresses with extra characters). The "search" method has already saved the search term, so changing the value there (in the input) is too late.
    – Matt Kemp
    Aug 26, 2015 at 3:27
12

The answer above is a great simple example and helped me get to this result on my app which generates an unordered list from the response data which displays the autocomplete results:

$("#input-search").autocomplete({
   source: function(request, response) {
      $.ajax({
         url: "/search/autocomplete",
         dataType: "json",
         data: {
            q: request.term
         },
         success: function(data) {
            response(data);
         }
      });
   },
}).data("autocomplete")._renderItem = function(ul, item) {
    $(ul).attr('id', 'search-autocomplete');
       return $("<li class=\""+item.type+"\"></li>")
       .data( "item.autocomplete", item )
       .append("<a href=\""+item.url+"\">"+item.title+"</a>").appendTo(ul);
    };

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.