-1

I am trying to change selected item from the list options dynamically at This Demo but it is not working.

<select id="selItems" class="selectpicker">
    <option>Select From List</option>
    <option>Mustard</option>
    <option>Ketchup</option>
    <option>Relish</option>
</select>
<p>
<button id="setdefault" type="submit" class="btn">Set Default</button>

<script>
 $("#setdefault").on("click", function () {
   $("#selItems option:eq(2)").attr("selected", "selected");
 });
</script>

can you please let me know what I am doing wrong?

isherwood
  • 58,414
  • 16
  • 114
  • 157
Suffii
  • 5,694
  • 15
  • 55
  • 92
  • Your `select` has an ID of `setdefault`... Therer is no `selItems` – tymeJV Sep 25 '14 at 19:39
  • thanks tymeJV , I fixed the code but still same thing happening! – Suffii Sep 25 '14 at 19:43
  • 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) – Joe Enos Sep 25 '14 at 19:53

5 Answers5

1

Bootstrap Select has a method for doing this. You shouldn't be manually manipulating attributes because the element seen in the page isn't the original select element anyway.

$("#setdefault").on("click", function () {
    $("#selItems").selectpicker('val', 'Ketchup');
});

Demo

http://silviomoreto.github.io/bootstrap-select/#methods

isherwood
  • 58,414
  • 16
  • 114
  • 157
1

There are several reasons why your code is not working how you expect it to.

You are setting a click event on your select element, and not on the "set default" button.

Your actual select element is not visible on the page because it is being replaced by the jquery plugin you are using.. so even if you set the selected attribute of your select, you won't see it on the page.

You should refer to the documentation of that plugin to figure out how it is used.

Jeromy French
  • 11,812
  • 19
  • 76
  • 129
Brian Glaz
  • 15,468
  • 4
  • 37
  • 55
1

Try using prop instead of attr:

 $("#setdefault").on("click", function () {
   $("#selItems option:eq(2)").prop("selected", true);
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="selItems" class="selectpicker">
    <option>Select From List</option>
    <option>Mustard</option>
    <option>Ketchup</option>
    <option>Relish</option>
</select>
<p>
<button id="setdefault" type="submit" class="btn">Set Default</button>
jrummell
  • 42,637
  • 17
  • 112
  • 171
0

Try switching

$("#setdefault").on("click",function(){
 $("#selItems option:eq(3)").attr("selected", "selected");
});

to

$("#setdefault").on("click",function(){
 $("#setdefault option:eq(3)").attr("selected", "selected");
});
Jack Shultz
  • 2,031
  • 2
  • 30
  • 53
0

Just use val():

$("#setdefault").on("click", function() {
  $("#selItems").val("Mustard");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="selItems" class="selectpicker">
  <option>Select From List</option>
  <option>Mustard</option>
  <option>Ketchup</option>
  <option>Relish</option>
</select>
<p>
<button id="setdefault" type="submit" class="btn">Set Default</button>
Rudolph Gottesheim
  • 1,671
  • 1
  • 17
  • 30