Saint Robson
Saint Robson

Reputation: 5525

Selected Country / State From Dynamic Dropdown List

I'm using this Country / State dynamic selection on my code :

http://bdhacker.wordpress.com/2009/11/21/adding-dropdown-country-state-list-dynamically-into-your-html-form-by-javascript/

and here's the demo page : http://bdhacker.sourceforge.net/javascript/countries/

but, unfortunately when I create setting / member profile page which I need to display selected country and state so user can see what they already chosen, I can't just put user's selected as 'value' on it.

here's what I see on page source even I have select a country and a state :

Select Country: <select onchange="print_state('state',this.selectedIndex);" id="country" name ="country"></select>

City/District/State: <select name ="state" id ="state"></select>

how to put value on that ? because I did like this :

Select Country:   
<select onchange="print_state('state',this.selectedIndex);" id="country" name ="country" value="Indonesia"></select>

City/District/State: <select name ="state" id ="state"></select>

and still I don't see there's a country selected. thanks!

Upvotes: 0

Views: 13553

Answers (2)

Thomas Gemal
Thomas Gemal

Reputation: 1

The reason you see the USA states for other countries is that the JavaScript is missing a reset of the state drop-down list. Just add "option_str.length=0; // Clear list" in the JS file before you split list array.

Upvotes: 0

br3t
br3t

Reputation: 1658

try this solution http://jsfiddle.net/yDAmR/

print_country("country");
$('#country').val('Indonesia');
print_state('state',$('#country')[0].selectedIndex);

Upvotes: 1

Related Questions