Add options dynamically to bootstrap-select

Author: Julian Hansen August, 2017

UPDATE: 2017-08-08 Tested with jQuery 3 you can see it here

UPDATE: 2017-08-08 The sample below has been updated to address some issues that were reported with it

The issue was caused by a change in the way the bootstrap-select markup is created by the plugin. In the version used by the previous version of this sample the <select> was placed immediately before the bootstrap-select container. In later versions of the plugin the <select> was moved inside the .bootstrap-select container thus breaking this solution

The code below has been updated to bring it in line with version v1.12.X of the bootstrap-select component

The principle change (apart from the upgrade to version 1.12 of the bootstrap select plugin) was the following line which changed from

var p=$(t).closest('.bootstrap-select').prev();
To
var p=$(t).closest('.bootstrap-select').find('select');

Other minor changes to markup were also made but do not affect the functionality

Acknowledgement

Thank you to Eric Nemchik for picking up the problem and suggesting some code changes

Select Sample #1

Select Sample #2