Skip to content
Fix Code Error

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

March 13, 2021 by Code Error
Posted By: Anonymous

I thought that adding a "value" attribute set on the <select> element below would cause the <option> containing my provided "value" to be selected by default:

<select name="hall" id="hall" value="3">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

However, this did not work as I had expected. How can I set which <option> element is selected by default?

Solution

Set selected="selected" for the option you want to be the default.

<option selected="selected">
3
</option>
Answered By: Anonymous

Related Articles

  • Angular HTTP GET with TypeScript error http.get(...).map is…
  • Create equal table layout regardless of the data inside
  • How to set HTML5 required attribute in Javascript?
  • How to resolve "Could not find schema information for the…
  • Compiler error: "class, interface, or enum expected"
  • How can I wrap all BeautifulSoup existing find/select…
  • Vue, compare two arrays and check if results match
  • For-each over an array in JavaScript
  • What are the currently supported CSS selectors available to…
  • Apply attribute-sets on existing elements

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Round to at most 2 decimal places (only if necessary)

Next Post:

How do I check if an element is hidden in jQuery?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error