I am working with a dropdown with countries.
Originaly I have that the selected item is Ukraine.
<select id="country" name="country">
<option value="US">USA</option>
<option value="UG">Uganda</option>
<option value="UA" selected>Ukraine</option>
<option value="AE">United Arab Emirates</option>
<option value="GB">United Kingdom</option>
</select>
With the next jQuery code I want to selected a different country:
$("select#country").val("GB");
In the dropdown now shows United Kingdom, but in the HTML still shows that Ukraine is the selected country, and I need that this change.
I test differents solutions but seems no one works:
I add .change() funtion, but the web start to make a infinity loop of refresing.
$("select#country").val(selectedOption).change();
I use different jQuery code, but all seems equals.
$("#country option[value="GB"]").attr('selected', 'selected');
I tried to use the .selectmenu('refresh') function but I need to update jQuery and I can't do that.
$("#country").selectmenu('refresh');
EDIT: The problem didn't was about jquery. It was problem how printed the HTML.
<xsl:for-each select="$country">
<xsl:attribute name="selected">
<xsl:text>selected</xsl:text>
</xsl:attribute>
</xsl:for-each>
First, I print the list choosing the selected country. After there was a sorting of the list (I didn't see before). This sorting change the list but not the selected country. It was a timing problem, like I mark in the correct answer.
Thx!