b11 b11 - 3 months ago 25
HTML Question

Cannot change title of Boostrap-select selectpicker

I want to programmatically change the title of my Bootstrap-select selectpicker.

I tried the following JS code which does not work.

$('#myPicker')
.selectpicker({title: 'New title'})
.selectpicker('render');


Any other ways I can do this?

Answer

Selectpicker will only update if the DOM has been updated. The function below should update title because it lets selectpicker append a new html object (DOM is changed)

function newTitle() {
    var selectpicker = $("#myPicker");
    selectpicker.selectpicker();
    selectpicker.selectpicker({title: 'New Title'}).selectpicker('render');
    html = '';
    selectpicker.html(html);
    selectpicker.selectpicker("refresh");
}