Skip to content Skip to sidebar Skip to footer

Is A Select Option With No Value, Valid?

Let's say that I have this select. If I post this, the value would be: city 1, city 2, city 3 and city 4. I don't want to give them a value, because the value is the same as the op

Solution 1:

If there is no value your text will be used. You can find this information on the HTML standard:

The value attribute provides a value for element. The value of an option element is the value of the value content attribute, if there is one, or, if there is not, the value of the element's text IDL attribute.
https://html.spec.whatwg.org/multipage/forms.html#the-option-element

or on the W3C Wiki

Provides a value for element. If there isn't, the value of an option element is the textContent of the element.
https://www.w3.org/wiki/HTML/Elements/option

Test Case

With the following snippet you can test the behaviour of the options: http://jsfiddle.net/ghqp08kr/

<form action="http://posttestserver.com/post.php" method="post">
  <select name="values">
    <option value="with-value">With Value</option>
    <option>Without Value</option>
    <option value="">With Empty Value</option>
  </select>
  <input type="submit"/>
</form>
<!-- With Value -->
key: 'values' value: 'with-value'

<!-- Without Value -->
key: 'values' value: 'Without Value'

<!-- Without Empty Value -->
key: 'values' value: ''

Solution 2:

While it is acceptable, it is not advisable as all the browsers and webservers interpret the same thing. It is always better to give values like this, even though they are just repeating:

<select>
    <option value="City 1">City 1</option>
    <option value="City 2">City 2</option>
    <option value="City 3">City 3</option>
    <option value="City 4">City 4</option>
</select>

From the docs:

value

The content of this attribute represents the value to be submitted with the form, should this option be selected. If this attribute is omitted, the value is taken from the text content of the option element.


Post a Comment for "Is A Select Option With No Value, Valid?"