I have some custom select-behaviour in my project, which adds some general functionality to select-boxes. I have implemented that they can be marked as optional, which allows the user to select the default value, which is empty.
Nowhere in the custom javascript code it ever manipulates the select's "value" property directly - it just manipulates the options, adding or removing the selected attribute/property. This works fine for all real options that have actual values - they get submitted with the value of their value-attribute.
However, when I select the default-option, which's value is empty (value=""), then the browser always sends the value of the 1st option upon form submit. I don't understand why. I can even confirm this with JavaScript.
<select id="stockWay" data-select-name="selectBox" data-optional="" name="stockWay" data-placeholder=“please choose ..." class="hide">
<option value=“FOO”>FOO</option>
<option value=“BAR”>BAR</option>
<option value="" selected="selected">please choose ...</option
</select>
This is the HTML of my select how it's on the site, that's copy pasted. When I call this JavaScript:
document.getElementById("stockWay").value
it returns "FOO". That doesn't make any sense to me, the empty-value option is clearly selected. And even if the empty value counts as "missing", it should use it's text-content according to the docs. I really don't get what is going on here. I thought the value-property on the select-element is a reflection of what option-children has the selected-attribute?? Clearly there is more going on, but I don't know what or where and how.
Does anyone have any hints where else I could be looking in order to find out how this happens? Any pointers would be greatly appreciated.
Edit: I have just further debugged this in chrome and I could see the following behaviour: The code calls
selectedOption.setAttribute("selected", "selected");
whenever an option gets selected. For all options with real values, this causes the property "selected" to become true. Only for the empty-value option, this does for some reason not make it become true. It simply remains false. It's literally one line of code. In both cases I can see that selectedOption is the expected element in the page. I don't get it.