Tim Kretschmer
Tim Kretschmer

Reputation: 2280

dropdown pre-select without fireing onchange

this is preselectiong my .item.active.selected but unfortunately it also fires the onChange. Is there another way?

$(function() {
  $.each($(".ui.dropdown"), function(i, dropdown) {
    selected_value = $(dropdown).find(".item.selected").data("value");
    $(dropdown).dropdown("set selected", selected_value);
  });

});

Upvotes: 2

Views: 1409

Answers (1)

AVK
AVK

Reputation: 645

Yes, you can set value and text instead.

So,

$(function() {
  $.each($(".ui.dropdown"), function(i, dropdown) {
    var val = $(dropdown).find(".item.active").data("value");
    var text = $(dropdown).find(".item.active").html();

    $(dropdown)
        .dropdown("set value", val)
        .dropdown("set text", text);
  });

});

But why do that?

You can pre-select a value by:

  1. Setting a value in <input type="hidden" value="my_val" /> field in div-based dropdowns
  2. Adding selected attribute to the option for select-based dropdowns

Upvotes: 5

Related Questions