Semantic-ui: [Dropdown] 'set selected' does not recognize dynamically added items?

Created on 13 May 2015  路  25Comments  路  Source: Semantic-Org/Semantic-UI

Hi,

Here's a fiddle to reproduce the issue, if it's an issue of course. I have doubts that I am doing something wrong here. But anyways...

Here's the problem:

  1. We have an existing dropdown
  2. menu items in dropdown change dynamically and we need to select a new value.
  3. But for some reason .dropdown("set selected", "new value") does not kick in
Usage Question

Most helpful comment

I had the same problema as achimkoellner.
The "set selected" didn't work after dynamically fill a select element.
I tried the "refresh" before the "set selected" method but still didn't work.
The only solution was to use a setTimeout to call "set selected" async.

for (var id in championships) {
var $option = $("<option value=\"" + id + "\">" + championships[id].name + "</option>");
$select.append($option);
}
$select.dropdown('refresh');
if (defaultValueId)
setTimeout(function () {
$select.dropdown('set selected', defaultValueId);
}, 1);

All 25 comments

Mutation observers (which refresh the selector cache) are asynchronous event callbacks which must wait for the browser to fire mutation events. If you need to use a value immediately you will manually have to call

$('.ui.dropdown').dropdown('refresh');

http://jsfiddle.net/51489p1k/

Understood. Thanks @jlukic

seems like an undocumented feature

Yes, there's a few features like that which are just designed to allow developers to be lazy and everything can 'just work'.

The refresh-method doesn't seem to work if the source element was a select element:

http://jsfiddle.net/nfsfb87y/1/

Couldn't find any documentation on set selected.
If I use a timeout after I append options to the select box it works.

I think the request method sould maybe return a promise such as

$("[name=productIds]").dropdown("refresh").then(function() {
    $("[name=productIds]").dropdown("set selected", "3"); 
});

I looked at the refreshSelectors method #4486 and to me it looks like a select element wouldn't be refreshed, is that possible?

I had the same problema as achimkoellner.
The "set selected" didn't work after dynamically fill a select element.
I tried the "refresh" before the "set selected" method but still didn't work.
The only solution was to use a setTimeout to call "set selected" async.

for (var id in championships) {
var $option = $("<option value=\"" + id + "\">" + championships[id].name + "</option>");
$select.append($option);
}
$select.dropdown('refresh');
if (defaultValueId)
setTimeout(function () {
$select.dropdown('set selected', defaultValueId);
}, 1);

same problem here with the