0

I add a "Placeholder" to a an existing select. When I select any option and then pick the blank item , the placeholder is not shown anymore. How to always show it whenever no option is selected?

var l = $("#my_select");
if (l.val() === '') {
    l.append($('<option>', { value: 1, text: 'Some Placeholder', disabled : true, selected: true, hidden: true }))
}
smolo
  • 737
  • 1
  • 15
  • 27

1 Answers1

0

You merged disabled and hidden together. Therefor the placeholder is in the list of options not selectable and not visible. If you just want it to be visible omit hidden. If you also want to reselect the placeholder omit both.

<select id="selected">
  <option value="" selected>placeholder</option>
  <option value="1">troet</option>
  <option value="2">quak</option>
</select>
<label for="selected">selected</label>
<br>
<select id="disabled">
  <option value="" selected disabled>placeholder</option>
  <option value="1">troet</option>
  <option value="2">quak</option>
</select>
<label for="disabled">disabled</label>
<br>
<select id="hidden">
  <option value="" selected hidden>placeholder</option>
  <option value="1">troet</option>
  <option value="2">quak</option>
</select>
<label for="hidden">hidden</label>
biberman
  • 5,606
  • 4
  • 11
  • 35