1

In express I am rendering a handlebar template populating a form with values.

To populate my select options I set the "value" property of my select node like so:

<select class="inputField" name="gender" value="female">
    <option value="male">Männlich</option>
    <option value="female">Weiblich</option>
</select>

Visually the male option is selected though. Can anyone explain why this is happening?

EDIT 1:

I am aware of the selected property, but it makes stuff more complicated when templating, so I'd rather pass down the selected value.

But to be more precise on my original question:

why does this work then:

var genderEl = document.getElementById('genderSelect');
    genderEl.value = "female";
Max Bumaye
  • 1,017
  • 10
  • 17

5 Answers5

3

the correct way to pre-select an option in a select input is:

<select class="inputField" name="gender">
    <option value="male">Männlich</option>
    <option value="female" selected>Weiblich</option>
</select>

if you are using xhtml or html4, you should change selected for selected="selected"

Erwin
  • 183
  • 1
  • 10
2

Why does this work then: var genderEl = document.getElementById('genderSelect'); genderEl.value = "female";

Simply because DOM uses different properties ;-)

To answer your original question: There is no value attribute for selects in HTML, but there is one in DOM. So you have to stick with the selected attribute, if you want a pure HTML solution.

Sebastian S
  • 4,420
  • 4
  • 34
  • 63
1

That's not how selects work. You need to use the selected attribute on an option, like this:

<select class="inputField" name="gender">
    <option value="male">Männlich</option>
    <option value="female" selected="1">Weiblich</option>
</select>
elixenide
  • 44,308
  • 16
  • 74
  • 100
1

I think that you are looking for something like this:

<select class="inputField" name="gender">
    <option value="male">Männlich</option>
    <option value="female" selected>Weiblich</option>
</select>
0

To select a option use:

<option value="female" selected="selected">Weiblich</option>

see

Community
  • 1
  • 1
cocoseis
  • 1,443
  • 1
  • 13
  • 35