snapappointments / bootstrap-select

:rocket: The jQuery plugin that brings select elements into the 21st century with intuitive multiselection, searching, and much more.
https://developer.snapappointments.com/bootstrap-select/
MIT License
9.85k stars 2.72k forks source link

Docs for selectpicker('val') #2530

Open jrichview opened 4 years ago

jrichview commented 4 years ago

The documentation for the selectpicker('val') function does not talk about setting the selected index rather than selected value. My first thought when reading it was "can I pass an number/index instead of the text value?".

As you know, sometimes you need to do one, and other times the other. It would be really helpful if there was some mention of setting the selected index programmatically in that part of the documentation even if you're just giving an example of setting it as you would with any plain-jane dropdown.

caseyjhol commented 4 years ago

This might be a good one to integrate (especially to more easily support an array of indices for a multiple select). For a single select, something like this should work:

var $select = $("#select-id");
$select[0].selectedIndex = 4;
$select.selectpicker("refresh");
jrichview commented 4 years ago

I find the following to be more JQUERY-friendly way to write it, but it's the same: $('#' + ddlDuration).prop('selectedIndex', 0).selectpicker('refresh');

jrichview commented 4 years ago

It sure would be nice if the selectpicker were able to hook the onchange of the hidden dropdown and know it needs to refresh auto-magically.