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

JavascriptWeb DevelopmentFront End Scripts

With HTML, you can easily create a simple drop down list of items to get user input in HTML forms. Use the <select> element for this, which is a select box, also called drop down box, with option to list down items.

Also, you can set the default value from the dropdown list of items in HTML forms. For that, add selected in the <option> tag for the value you want to preselect.

Example

You can try to run the following code to learn how to set the default value for HTML <select> element −

Live Demo

<!DOCTYPE html>
<html>
   <head>
      <title>HTML Select Element</title>
   </head>
   <body>
      <p> Select any one:</p>
      <form>
         <select name = "dropdown">
            <option value = "Java">Java</option>
            <option value = "Discrete Mathematics" selected >Discrete Mathematics</option>
            <option value = "Chemistry">Chemistry</option>
         </select>
      </form>
   </body>
</html>
raja
Published on 07-Feb-2018 12:54:11
Advertisements