Open In App

How to set the default value for an HTML <select> element ?

Last Updated : 16 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Setting the default value for an HTML <select> element ensures a specific option is preselected when the form loads. This is done by adding the selected attribute to the desired <option> tag. It’s useful for enhancing user experience by pre-filling forms with common or expected values.

The <select> tag creates dropdown lists; <option> tags define selectable values. 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. We can even set the selected attribute after the page is loaded with the help of Javascript.

Syntax:

<option value="value" selected>Option Name</option>

Example 1: This example illustrates the use of the selected attribute by specifying the pre-selected option that will be displayed first from the drop-down list.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Set the default value for &lt;select&gt; element</title>
</head>

<body>
    <b>
        How to set the default
        value for an HTML &lt;select&gt; element?
    </b>
    <p>The Starter option will be selected by default</p>

    <p>Choose your plan below:</p>

    <select name="plan" id="plan">
        <option value="free">Free</option>
        <option value="starter" selected>Starter </option>
        <option value="professional">Professional</option>
        <option value="corporate">Corporate</option>
    </select>
</body>

</html>

Output:

set-html-select-default-value

set html select default value Example Output

Example 2: This can also be used to add messages like ‘Select an Option’ in the list. This option would have the hidden and disabled attribute in addition to selected.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Set the default value for &lt;select&gt; element</title>
</head>

<body>
    <b>How to set the default value for an 
       HTML &lt;select&gt; element?
    </b>
    <p>The Select an Option would be shown by default.</p>

    <p>Choose your plan below:</p>

    <select name="plan" id="plan">
        <option value="none" selected disabled hidden>Select an Option</option>
        <option value="free">Free</option>
        <option value="starter">Starter </option>
        <option value="professional">Professional</option>
        <option value="corporate">Corporate</option>
    </select>
</body>

</html>

Output:

set-html-select-default-value-2

set html select default value Example Output


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads