Loading...
Loading...
00:00:00

<select>, <option> and <optgroup> tag in HTML5

The <select> and <option> elements in HTML5 are used to create a drop-down list of options that a user can select from.

The <select> element defines the drop-down list, while the <option> element defines each individual item in the list. The value attribute of the <option> element specifies the value that will be sent to the server when the form is submitted, while the text between the <option> tags is displayed to the user.

Here is an example of a simple drop-down list in HTML5:

<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

In this example, a drop-down list is created with four options: "Volvo", "Saab", "Mercedes", and "Audi". The user can select one of these options, and the value of the selected option will be sent to the server when the form is submitted.

The <select> element can also be used with the multiple attribute, which allows the user to select more than one option at a time. In this case, the name attribute of the <select> element should have square brackets ([]) appended to it to indicate that multiple values can be sent.

In addition to these basic usage, the <select> and <option> elements have several attributes that can be used to customize their behavior, such as the disabled, selected, and label attributes.

optgroup tag in select

The <optgroup> element in HTML5 is used to group related options together within a drop-down list. The <optgroup> element is placed within a <select> element, and contains one or more <option> elements.

The label attribute of the <optgroup> element specifies the label for the group of options, which is displayed to the user. When a user selects an option from the drop-down list, the selected option and its group label are both sent to the server when the form is submitted.

Here is an example of a drop-down list with grouped options in HTML5:

<select>
  <optgroup label="Swedish Cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </optgroup>
  <optgroup label="German Cars">
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </optgroup>
</select>

In this example, two groups of options are created, "Swedish Cars" and "German Cars". Each group contains two options, and the options within each group are related to one another. The user can select one of the options from the drop-down list, and the selected option and its group label are both sent to the server when the form is submitted.

The <optgroup> element can be useful when a drop-down list contains a large number of options, as it helps to organize the options and make the list easier to navigate for the user.