Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is a select option with no value, valid?

Tags:

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 option-name.

Is this okay?

<select>
    <option>City 1</option>
    <option>City 2</option>
    <option>City 3</option>
    <option>City 4</option>
</select>
like image 797
Niels Hermann Avatar asked Dec 14 '15 11:12

Niels Hermann


People also ask

Does select need value?

No, there is no such requirement.

Does select tag have value?

select elements do not have a value attribute.

What does it mean when the value of the select menu option is an empty string?

</option> , value="" is turned into just value . The value, when set to the empty string is simply discarded.


1 Answers

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: ''
like image 83
Sebastian Brosch Avatar answered Sep 19 '22 12:09

Sebastian Brosch