dropdown.js
dropdown.js copied to clipboard
How to change which item is selected?
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.
You can change the selection in the select element and trigger the change event
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"});
});
What about
$(".select").val("x").trigger("change");
$(".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..
It was working, looks like some commit removed the feature..
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..
Feel free to send a PR
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.
nekkon's fix still works