0

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:

  1. I add .change() funtion, but the web start to make a infinity loop of refresing.

    $("select#country").val(selectedOption).change();
    
  2. I use different jQuery code, but all seems equals.

    $("#country option[value="GB"]").attr('selected', 'selected');
    
  3. 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!

AlvaroRM
  • 5
  • 8

2 Answers2

0

Use trigger('change');

$('#btn1').click(function() {
  $('#country').val('UG').trigger('change');
});
$('#btn2').click(function() {
  $('#country').val('AE').trigger('change');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
<button id='btn1' />1</button>
<button id='btn2' value='2'>2</button>
4b0
  • 21,981
  • 30
  • 95
  • 142
0

I guess you have a problem with timing. The select must exist when you run the script. Also jQuery must exist when you run the script.

No change event trigger is required, it never is for normal select value changes.

https://jsfiddle.net/k1m8ayy5/

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
<script>$("#country").val("GB")</script>

A side note: If you have an ID, you never need a tag filter, $('#id') is enough. ID is per defination unique on a page, or at least should be unique on a page.

Brain Foo Long
  • 2,057
  • 23
  • 28