How to allow only one item selected in a multiple select with HTML?

Spread the love

Sometimes, we want to allow only one item selected in a multiple select with HTML.

In this article, we’ll look at how to allow only one item selected in a multiple select with HTML.

How to allow only one item selected in a multiple select with HTML?

To allow only one item selected in a multiple select with HTML, we remove the multiple attribute from the select.

For instance, we write

<select name="user" id="userID" size="3">
  <option>John</option>
  <option>Paul</option>
  <option>Ringo</option>
  <option>George</option>
</select>

to remove the multiple attribute to stop multiple values from being selected.

We set the size attribute to 3 to show 3 options in the box at a time.

Conclusion

To allow only one item selected in a multiple select with HTML, we remove the multiple attribute from the select.

Leave a Reply

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