Set a select html default field

Asked

Viewed 113 times

0

I would like to know how to leave the first option of a combobox (in html) with the option "Select" And force the user to select at least 1 field in the checkbox.

Example:

<select name="endereco_estado">
  <option value="AC">Acre</option>
  <option value="AL">Alagoas</option>
  <option value="AP">Amapá</option>

Thank you

1 answer

1

To force the user to select at least one value in inputs HTML, you need to use the attribute required, as follows:

<select name="endereco_estado" required="required">
  <option value="AC">Acre</option>
  <option value="AL">Alagoas</option>
  <option value="AP">Amapá</option>
</select>

A solution to add this first option by using only HTML (without CSS or Javascript) would be using a option with the attributes disabled and Selected. This would make her appear at the top, since the attribute Selected causes it to be selected automatically, and could not be selected by the user due to the attribute disabled:"

<select name="endereco_estado" required="required">
  <option value="" disabled selected>Selecione</option>
  <option value="AC">Acre</option>
  <option value="AL">Alagoas</option>
  <option value="AP">Amapá</option>
</select>

See working on repl.it

  • 1

    Thank you very much John!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.