In this section, we will learn what the required attribute is in HTML <select> element and how it works.
What is required Attribute in HTML <select> Tag?
People are lazy by nature! Sometime you have a form in an HTML document with a dropdown list. But some of the users might just jump over the list and don’t select any value! However, we want absolute users to select a value from the list with no exception!
This is where the required attribute comes in.
If we apply this attribute to a <select> element, the element becomes required for users to pick one.
This means as long as users don’t select an item from the list, none of the form data will be submitted to a server.
Also, browsers show an error message, which says the user needs to select an item from the list before submitting the form data.
HTML required Attribute in <select> Tag Syntax:
<select required> </select>
<select> Tag required Attribute Values
The required attribute does not need a value. Just put the value in the opening tag of <select> element and that’s it. The target dropdown list becomes required.
Example: using required attribute in HTML <select> tag
See the Pen using required attribute in HTML <select> tag by Omid Dehghan (@odehghan) on CodePen.