How to change which item is selected? #14

Open
Kirkman opened this Issue Apr 27, 2015 · 11 comments

Projects

None yet

8 participants

Kirkman commented Apr 27, 2015

I am using a dropdown together with a clickable map. If someone clicks an item in the map, I want to update the <select> list to show that item as being selected.

Doing this doesn't seem to work:

$('#map-container .sample select').val("3");

Yes I was wondering if there was a way to programmatically change selected option.

Owner

You can change the selection in the select element and trigger the change event

adpauly commented Jul 24, 2015

I tried these following but it doesn't work, did I miss something?

    $('.select-dropdown').on('change', function() {
            $(".select-dropdown").dropdown();
    });
    $('.select-dropdown').on('change', function() {
            $(".select-dropdown").dropdown({ "dropdownClass": "select-dropdown"});
    });
Owner

What about

$(".select").val("x").trigger("change");
Contributor
nekkon commented Aug 3, 2015

$(".select").val("x").trigger("change"); // Does not work..

$(".select").val(); // gets the value
$(".select").val("x").trigger("change"); // does not set it..

Please help asap..

Owner

It was working, looks like some commit removed the feature..

Contributor
nekkon commented Aug 4, 2015

Had to fix it by myself, please support your plugin.. I'll be using it at my projects.

Fix:

$select.on("change", function(e) {
          var $this = $(e.target);
          if (!$this.val().length) return;

          if (!multi) {
            var $selected;
            if ($select.find(":selected").length) {
              $selected = $select.find(":selected").last();
            }
            else {
              $selected = $select.find("option, li").first();
              // $selected = $select.find("option").first();
            }
            methods._select($dropdown, $selected);
          } else {
            methods._select($dropdown, $select.find(":selected"));
          }
        });

Add this on line 170 in the .js file and $(".select").val("x").trigger("change"); will work..

Owner

Feel free to send a PR

Nicks182 commented Oct 7, 2015

Thanks for the fix nekkon.

I got the same issue. In my case, I cannot use .trigger("change") because I want 2 selects are both the same value when each one changed. How should I do?

Does this fix address when no item is selected?

selectedIndex = -1

Doesn't seem to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment