Search code examples
javascriptjqueryautosuggest

Change query argument of jQuery suggest plugin


This question is kind-of crappy because I try to get around some limitations:

Current JS sends an ajax query with the following code

jQuery('#searchbox').suggest('/?live=1');

What the server get is the following query string:

?live=1&q=searchstring

Problem: The server expects the query string to be preceded with 's=' not 'q='

I have to use the existing scripts so what I'm trying to so is find a way to change 'q=' to 's=' in javascript, without altering the exisiting suggest plugin or the php search script.

Thanks.


Solution

  • The only way you will do that is by modifying the plugin, if you want to avoid changing the script forever and need this only for one page, override the function temporarily.

    $.suggest.suggest = function() {
        var q = $.trim($input.val());
        if (q.length >= options.minchars) {
            cached = checkCache(q);
            if (cached) {
                displayItems(cached['items']);
            } else {
                    //This is the line we r changing
                    $.get(options.source, {s: q}, function(txt) {
                    $results.hide();
                    var items = parseTxt(txt, q);
                    displayItems(items);
                    addToCache(q, items, txt.length);
                });
            }
        } else {
            $results.hide();
        }
    }