0

So i have two different inputs. First one has all the necessary options set by default but the second one has its options set based on what use chooses in the first one.(lets call them "countries" and "cities").

After Country is selected and its cities are loaded i would like to set value of city. I do it like this:

$('#city_id').val(cityVariable)

but it fails to load everytime. I'm almost sure its because DOM is reloaded but i still dont know how to refresh it properly. Long story short i want to be able to select option out of options downloaded with ajax

UPDATE

to make it more clear i will give you pseudo code ofhow it works

$('#country_id').change ->

    ///here goes ajax and the options for city select are called

    /////here DOM should be refreshed so i can use new options

    $('#city_id').val(cityVariable) 
Leo
  • 2,061
  • 4
  • 30
  • 58

1 Answers1

1

Well, I've made this demo sample, so we could see that it basically works. After discussion with TO, it appeared that he called

$('#city_id').val(cityVariable); 

after ajax call, but not inside success callback. Since AJAX is asynchronous by its nature, by the time the above statement was called, there was no content in $('#city_id') select - it was populated inside ajax successful callback. So moving above statement inside callback did the trick.

Ilya Luzyanin
  • 7,910
  • 4
  • 29
  • 49