brsbilgic
brsbilgic

Reputation: 11833

jQuery AJAX initial data problem for dynamic dropdowns

I have a problem with initialization of dynamically filled dropdowns in jQuery. Basically, I have function fillCityList and it makes AJAX call to fill the cities by the passing country. Because this is used in Edit form, I have a default City value in id_cityHidden field. Actually the code below works well.However, because fillCityList takes long time to fill the city list, while default city is selected, the city list may not be ready.

$(document).ready( function() {
    fillCityList(1);
    $('#city').val($("#id_cityHidden").val());  
});

I know there is a solution like "call function at complete stage of AJAX call" but I just need it during initialization. One solution might be setting a timeout or delay between fillCityList and $('#city').val($("#id_cityHidden").val()) however, it is not a good solution of course.

What is the best way to do this ?

Thanks

Upvotes: 0

Views: 134

Answers (1)

Tank
Tank

Reputation: 1006

I would suggest you build your ajax to use the 'complete' function, then update the value.

http://api.jquery.com/ajaxComplete/

Upvotes: 1

Related Questions