Cannot change title of Boostrap-select selectpicker
Asked Answered
U

2

7

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?

Upsilon answered 31/8, 2016 at 18:30 Comment(0)
I
10

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");
}
Imperator answered 31/8, 2016 at 20:16 Comment(0)
D
5

The posted code does seem to work in the snippet.

$(document).ready(function() {
  $('#myPicker2')
    .selectpicker({
      title: 'New title'
    });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.11.0/css/bootstrap-select.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.11.0/js/bootstrap-select.min.js"></script>

<select id="myPicker1" class="selectpicker" multiple title="Choose one of the following...">
  <option>Mustard</option>
  <option>Ketchup</option>
  <option>Relish</option>
</select>

<select id="myPicker2" class="selectpicker" multiple title="Choose one of the following...">
  <option>Mustard</option>
  <option>Ketchup</option>
  <option>Relish</option>
</select>
Donau answered 31/8, 2016 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.