HTML - attributes - selected

revision:


Content

"selected" attribute : a boolean attribute specifying a pre-selected option syntax some examples


"selected" attribute : a boolean attribute specifying a pre-selected option

top

When present, it specifies that an option should be pre-selected when the page loads. The pre-selected option will be displayed first in the drop-down list. The selected attribute can also be set after the page loads, with a JavaScript.
With multiple options selected, only the last option with this attribute will be selected. However, a multi-select dropdown control, with the "multiple" attribute, can have multiple options selected.


syntax

top

<option selected></option>

This attribute has no values. Specifying the "selected" attribute with an arbitrary value has the same effect as specifying it with no value. For example, all of the following declarations have the same effect: selected, selected="true", selected="false", selected="on", selected="selected".


some examples

top
codes
                    <label style="margin-left:5vw;" for="cars">Choose a car:</label>
                    <select id="cars">
                        <option value="volvo">Volvo</option>
                        <option value="saab">Saab</option>
                        <option value="vw">VW</option>
                        <option value="audi" selected>Audi</option>
                    </select>
                
codes:
                    <label style="margin-left:5vw;" for="pet-select">Choose a pet:</label>
                    <select name="pets" id="pet-select">
                        <option value="dog">Dog</option>
                        <option value="cat">Cat</option>
                        <option value="hamster">Hamster</option>
                        <option value="parrot">Parrot</option>
                        <option value="spider" selected>Spider</option>
                        <option value="goldfish">Goldfish</option>
                    </select>