Jquery - Setting form select-field "selected" value only works after refresh
- by frequent
Hi,
I want to use a form select-field for users to select their country of residence, which shows the IP based country as default value (plus the IP address next to it);
Location info (ipinfodb.com) is working. I'm passing "country" and "ip" to the function, which should modify select-field and ip adress
Problem:
IP adress works, but the select-field only updates after I hit refresh.
Can someone tell me why?
Here is the code:
HTML
<select name="setup_changeCountry" id="setup_changeCountry">
<option value="AL-Albania">Albanien</option>
<option value="AD-Andorra">Andorra</option>
<option value="AM-Armenia">Armenien</option>
<option value="AU-Australia">Australien</option>
...
</select>
<div class="setup_IPInfo">
<span>Your IP</span>
<span class="ipAdress"> -- ip --</span>
</div>
Javascript/Jquery
function morph(country,ip) >> passed from function, called on DOMContentLoaded
{
var ipAdress = ip;
$('.ipAdress').text(ipAdress);
var countryForm = country;
$('#setup_changeCountry option').each(function()
{
if ($(this).val().substr(0,2) == countryForm)
{
$(this).attr('selected', "true");
}
});
}
Thanks for any clues on how to fix this.
Frequent