Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

value attribute on <select> tag not selecting default option

Tags:

html

Perhaps i'm misunderstanding here, but given the following html:

<select value="2">
    <option value="1">Something</option>
    <option value="2">Something else</option>
</select>

I would expect "Something else" to be the default selected option. However, it does not seem to be. Why is this, and what should I be doing differently?

like image 513
crazy2be Avatar asked Apr 08 '11 01:04

crazy2be


People also ask

How do I set the default selected value in select?

The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.

Can select tag have value attribute?

select elements do not have a value attribute.

What is value attribute in select tag?

The value attribute specifies the value to be sent to a server when a form is submitted. The content between the opening <option> and closing </option> tags is what the browsers will display in a drop-down list. However, the value of the value attribute is what will be sent to the server when a form is submitted.


1 Answers

You use selected attribute on an option element to specify default option.

<select>
    <option value="1">Something</option>
    <option value="2" selected="selected">Something else</option> // this is default
</select>

select elements do not have a value attribute.

like image 119
Genzer Avatar answered Nov 07 '22 03:11

Genzer