22

I'm trying to pass extra parameters for city and state using the jQuery UI autocomplete function. I've been trying to find an answer to this for a while but can't seem to find something that works for me.

My current code is:

$(document).ready(function () {
    $("#id_place").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "/autocomplete_place",
                dataType: "json",
                data: {
                    term: request.term,
                    city: $("id_city").val(), 
                    state: $("id_state").val(),
                    test: 4
                },
                success: function(data) {
                    response(data);
                }
            });
        },
    });
});

The autocomplete works, but its not passing my city and state parameters to the function. If I type v it requests the URL: /autocomplete_place?term=v&test=4

I'm guessing its evaluating the val() of city and state upon (document).ready() and getting blank values for these form fields? I thought making source into an ajax function would solve that, but perhaps not.

Any ideas?

1
  • 1
    I wanted to do this in rails 3.1 finally figured it out and put it in a Gist on github Commented Feb 20, 2012 at 19:43

1 Answer 1

9

Are you missing a # in your selector $("#id_city").val()?

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

2 Comments

Wow, I actually was missing it. Haha, hours of trying to figure this out and that was the error. Thanks!
I think every programmer has been there some time. I have personally spent days in the past debugging just to find errors like this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.