HTML selected attribute

HTML selected attribute selects one option out of many options by default.

<p>This attribute gives the default or initial value to the select element.</p>
<p>If you are given a chance to visit one of the following countries,then what will be your choice?</p>
<select>
 <option>Russia
 <option>Qatar
 <option selected>Algeria
 <option>Uganda
</select>
</>

This attribute gives the default or initial value to the select element.

If you are given a chance to visit one of the following countries,then what will be your choice?

In the above example, the Algeria option is selected instead of Russia i.e. the user sees the Algeria option initially.

Attribute values

All possible values of selected attribute

selected attribute is a boolean type attribute. The presence of this attribute selects one option in an options list.

By default the value of the selected attribute is false. The value can be set to true by writing the selected attribute in the respective option element.

Related Tags

Related Tags

The tags that support selected attribute
<option> tag

option tag defines one option. If selected attribute is used in more than one options elements in an options list, then last option having the selected attribute will be selected.

<select>
 <option>USA
 <option>UK
 <option selected>China
 <option selected>Australia
</select>
Try</>

In the above example, although selected attribute is used in two options but the last one will be selected (i.e. Australia).

This attribute should be used only once in an option list.



Was this article helpful?

 

Email:

Message: