49 of 133 menu

The select tag

The select tag creates a drop-down list for use in HTML forms.

A single list item should be stored in a option tag.

Attributes

Attribute Description
multiple The presence of this attribute creates a multiselect - a drop-down list in which you can select several items by holding down the Ctrl key or by selecting them with the mouse.
Optional attribute.
name The name of the drop-down list. This is needed to get the input field data into PHP. For the form to work correctly, the names of the input fields should not match each other (in one form). If they match, the data of the input field that is lower in the HTML code will come to PHP.

Example

Let's see how the drop-down list works:

<select> <option>City1</option> <option>City2</option> <option>City3</option> <option>City4</option> </select>

:

Example

Let's make the width of the drop-down list equal to the width of the largest element (unless it is explicitly specified using the CSS property width):

<select> <option>Big City1</option> <option>City2</option> <option>City3</option> <option>City4</option> </select>

:

Example . Default selection

Now let's try to select a default city. We'll do this using the selected attribute:

<select> <option>Big City1</option> <option selected>City2</option> <option>City3</option> <option>City4</option> </select>

:

Example . Multiselect

Let's now turn a regular select into a multiselect using the multiple attribute:

<select multiple name="city[]"> <option>City1</option> <option>City2</option> <option>City3</option> <option>City4</option> </select>

:

Please note that the select name specified in the name attribute must have square brackets at the end. This is necessary so that all selected values ​​will come to PHP (otherwise only one will come - the last one).

Example . Multiple default values ​​in multiselect

Now, in the default multiselect, let's try to select more than one value:

<select multiple name="city[]"> <option>City1</option> <option selected>City2</option> <option>City3</option> <option selected>City4</option> </select>

:

See also

  • attribute selected,
    which sets the default selected list item
  • tag optgroup,
    which groups list items
  • tag datalist,
    which creates a drop down list
byenru