Bootprap selectpicker "data-subtext" does not update live changes

I want to change my data-subtext

attribute of option

my select

element at runtime. But data-subtext

not updated in selectpicker. Here's my code:

$('.selectpicker').attr("data-subtext","new subtext").selectpicker('refresh');

      

+3


source to share


1 answer


This is because a class .selectpicker

designates an element select

by itself is not for it option

s. Considering that it can have more than one option

, you need to specify which one option

data-subtext

you want to change. refer to the following snippet:



$(document).ready(function() {
  $('#myBtn').click(function() {
    $(".selectpicker > option:eq(1)").data("subtext", "Look I'm changed");
    $(".selectpicker").selectpicker('refresh');
  });
});
      

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" />
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.5/css/bootstrap-select.min.css" />
  <link rel="stylesheet" href="style.css" />
  <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.5/js/bootstrap-select.min.js"></script>

 <select class="selectpicker">
        <option data-subtext="Mustard subtext">Mustard</option>
        <option data-subtext="Ketchup subtext">Ketchup</option>
        <option data-subtext="Relish subtext">Relish</option>
      </select>
      <a class="btn btn-default" id="myBtn">Update second options data-subtext</a>
      

Run codeHide result


+3


source







All Articles