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

22 Nov, 2021
In this article, we will learn about setting the default value for the HTML select element, & will also understand their implementation through the examples.
The select tag in HTML is used to create a dropdown list of options that can be selected. The option tag contains the value that would be used when selected. 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 <select> element</title></head>  <body>    <h1 style="color: green">GeeksforGeeks</h1>    <b>        How to set the default         value for an HTML <select> 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:
selected Attribute
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 <select> element</title></head>  <body>    <h1 style="color: green">GeeksforGeeks</h1>    <b>How to set the default value for an        HTML <select> 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:
Disabled selected Attribute
HTML is the foundation of web pages, is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.
bhaskargeeksforgeeks
HTML-Questions
Picked
HTML
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
REST API (Introduction)
How to Insert Form Data into Database using PHP ?
HTML Cheat Sheet - A Basic Guide to HTML
Design a web page using HTML and CSS
HTTP headers | Content-Type
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript