NoobDev4iPhone
NoobDev4iPhone

Reputation: 5729

jQuery autocomplete: How to complete multiple fields when entering data into one of them?

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)

Upvotes: 2

Views: 2028

Answers (2)

user425367
user425367

Reputation:

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>

Upvotes: 0

Jan Dragsbaek
Jan Dragsbaek

Reputation: 8101

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

Upvotes: 2

Related Questions