2

For my site I need the registering users to enter their zipcode and city, and entering a city should be enough for the page to auto-complete the zipcode.

Let's say I have both fields:

<input id="city" name="city_field" type="text">
<input id="zip" name="zip_field" type="text">

To auto-complete the city, I have this code, which works perfectly fine:

<script type="text/javascript">
    var data = ['New York', '...']; //I keep the entire list here
    $(document).ready(function(){
        $("#city").autocomplete({
            source: data
        });
    });
</script>

Now, how do I auto-complete the zipcode right when the city is entered?

(I keep all of the data in variables, so there is no need to connect to database)

2 Answers 2

2

You need to use the event-select thats available in autocomplete.

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

2 Comments

Since I'm new to jQuery, can you please give a snippet code so that I know what to do exactly. I'll greatly appreciate it.
Id suggest you to actually try and play around with it instead of asking us for snippets.
0

Something like this

<script type="text/javascript">
var data = ['New York', '...']; //I keep the entire list here
$(document).ready(function(){
    $("#city").autocomplete({
        source: data,
        select: function(event, ui) {
            $("input#city").val(ui.item.value);
            $("input#zip").val(foo(ui.item.value)); // TODO Map to zip
            return false;
        },
    });
});
</script>

Comments

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.