I have a bootstrap select which i want to change the value programmatically. Something just like:
<select class="form-control selectpicker" id="testpicker" data-lastselected="Ketchup">
<option>Mustard</option>
<option selected="">Ketchup</option>
<option>Relish</option>
</select>
To change it i use the following function:
$('#testpicker').selectpicker('val', value to change);
And i'm listening to it's change with:
$('select').on("change", function() {}
Inside the change function i've a if statement asking the user if he really wants to change the option and proceed with the process:
if (!confirm("Sure you want to change to " + $('#testpicker').val())) {
$('#testpicker').selectpicker('val',
$('#testpicker').data('lastselected'));
} else {
$('#testpicker').data('lastselected', $('#testpicker').selectpicker('val'));
}
So, when the users response to the confirm is cancel, the bootstrap-select enters in a infinite loop, always showing the confirm dialog until the users accept it!
You can check it here.
Solution i've used to workaround this behaviour - to change the value use:
$('#testpicker').val(value to change).selectpicker('refresh');