5

I am using the Bootstrap-Select:

<select name="SelectAddress" class="selectpicker">
   <option value="a">Val 1</option>
   <option value="b">Val 2</option>
   <option value="c">Val 3</option>
   <option value="d">Val 4</option>
</select>

For example, I want to change Val 1 to Val 1.0 knowing Val 1 is selected. So I tried with no success:

$("select[name=SelectAddress]").text("Val 1.0");
$("select[name=SelectAddress]").selectpicker("refresh");

How can I achieve this?

Edit:

Found a solution, by inserting an ID on each <option>:

<select name="SelectAddress" class="selectpicker">
   <option id="1" value="a">Val 1</option>
   <option id="2" value="b">Val 2</option>
   <option id="3" value="c">Val 3</option>
   <option id="4" value="d">Val 4</option>
</select>

Then:

$("#1").html("Val 1.0");
$("select[name=SelectAddress]").selectpicker("refresh");
London Smith
  • 1,622
  • 2
  • 18
  • 39
  • http://stackoverflow.com/questions/14804253/how-to-set-selected-value-on-select-using-selectpicker-plugin-from-bootstrap is this duplicate of this?? – Dipak Thoke Oct 24 '16 at 17:47
  • No, I don't want to change the value, but what is in `` – London Smith Oct 24 '16 at 17:48
  • please try below given solution... – Dipak Thoke Oct 24 '16 at 17:58
  • this is it not efficient way giving id to each option you can use value attribute to set value of the option. – Dipak Thoke Oct 24 '16 at 18:01
  • Possible duplicate of [How to set selected value on select using selectpicker plugin from bootstrap](http://stackoverflow.com/questions/14804253/how-to-set-selected-value-on-select-using-selectpicker-plugin-from-bootstrap) – Ion May 12 '17 at 09:05

1 Answers1

7

You can do like this.

<select id="drop-down" name="SelectAddress" class="selectpicker">
   <option value="a">Val 1</option>
   <option value="b">Val 2</option>
   <option value="c">Val 3</option>
   <option value="d">Val 4</option>
</select>



$("#drop-down option[value='a']").text("Val 1.0");
$("select[name=SelectAddress]").selectpicker("refresh");
Dipak Thoke
  • 1,963
  • 11
  • 18
  • 1
    +1 Thanks this line solved my problem by putting it inside onShown:void(){ $("select[name=SelectAddress]").selectpicker("refresh"); } as I am using angular2 and JQuery selectpicker. and in html – sohaib javed Mar 26 '17 at 10:09
  • @sohaibjaved, if it work for you then please vote for me Thanks. – Dipak Thoke Mar 26 '17 at 13:50