Languages
[Edit]
EN

HTML - set default value for select element

0 points
Created by:
Marley-Marks
442

In this article, we would like to show you how to set a default value for select element in HTML.

Quick solution:

// ONLINE-RUNNER:browser;

<select>
  <option value="option-value" selected >Default option</option>
</select>

 

Practical example

In this section, we present a practical example of how to set a default value for a select element (select box) using selected attribute.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <form name="my-form">
    <label>Select fruit: </label>
    <select name="my-fruits">
      <option value="apple" selected >🍎 Apple</option>
      <option value="banana">🍌 Banana</option>
      <option value="grape">🍇 Grape</option>
    </select>
  </form>
</body>
</html>

References

  1. <option>: The HTML Option element - HTML | MDN
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join