0

I want to use the jquery ui autocomplete plugin with 2 controls, so I have this:

    $("#From, #To").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "http://example.com/search/" + $(this).val(),
                dataType: "jsonp",
                data: {
                    featureClass: "P"
                },
                success: function (data) {
                    response($.map(data.autocomplete, function (item) {
                        return {
                            label: item.name + " (" + item.id + ")",
                            value: item.id
                        }
                    }));
                }
            });
        }
    });

The problem is that the url param where I use $(this).val() to get the text in the current textbox doesn't work, how can I do this in a way that I won't need to duplicate the autocomplete code for each control?

thanks!

1
  • That looks ok at first glance, put a breakpoint on it and run $(this).attr("id") to verify this is pointing to the correct object. Commented Feb 2, 2012 at 13:41

1 Answer 1

1

You can use this.term to get the search term that is to be used. You might also want to wrap this in encodeURIComponent() i.e.

url: "http://example.com/search/" + encodeURIComponent(this.term),
... // etc.
Sign up to request clarification or add additional context in comments.

1 Comment

In this case this refers to the jQuery UI widget, i.e. the autocomplete instance. The term property is just set up by the autocomplete widget internally to hold the search term to use to filter down the auto-complete options. I just did a console.log(this); within the success function to find it :-)

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.