Components

Select

The select component allows users to choose from a list of available options.

<div class="govuk-form-group">
<label class="govuk-label" for="sort"> Sort by </label>
<select class="govuk-select" id="sort" name="sort">
<option value="published">Recently published</option>
<option value="updated" selected>Recently updated</option>
<option value="views">Most views</option>
<option value="comments">Most comments</option>
</select>
</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you’re using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
id string Required.

ID for each select box.

name string Required.

Name property for the select.

items array Required.

The items within the select component.

See items.

value string

Value for the option which should be selected. Use this as an alternative to setting the selected option on each individual item.

disabled boolean

If true, select box will be disabled. Use the disabled option on each individual item to only disable certain options.

describedBy string

One or more element IDs to add to the aria-describedby attribute, used to provide additional descriptive information for screenreader users.

label object Required.

The label used by the select component.

See label.

hint object

Can be used to add a hint to the select component.

See hint.

errorMessage object

Can be used to add an error message to the select component. The error message component will not display if you use a falsy value for errorMessage, for example false or null.

See error message.

formGroup object

Additional options for the form group containing the select component.

See formGroup.

classes string

Classes to add to the select.

attributes object

HTML attributes (for example data attributes) to add to the select.

Options for formGroup
Name Type Description
classes string

Classes to add to the form group (for example to show error state for the whole group).

attributes object

HTML attributes (for example data attributes) to add to the form group.

beforeInput object

Content to add before the select used by the select component.

See beforeInput.

afterInput object

Content to add after the select used by the select component.

See afterInput.

Options for afterInput
Name Type Description
text string Required.

Text to add after the select. If html is provided, the text option will be ignored.

html string Required.

HTML to add after the select. If html is provided, the text option will be ignored.

Options for beforeInput
Name Type Description
text string Required.

Text to add before the select. If html is provided, the text option will be ignored.

html string Required.

HTML to add before the select. If html is provided, the text option will be ignored.

Options for hint
Name Type Description
text string Required.

If html is set, this is not required. Text to use within the hint. If html is provided, the text option will be ignored.

html string Required.

If text is set, this is not required. HTML to use within the hint. If html is provided, the text option will be ignored.

id string

Optional ID attribute to add to the hint span tag.

classes string

Classes to add to the hint span tag.

attributes object

HTML attributes (for example data attributes) to add to the hint span tag.

Options for items
Name Type Description
value string

Value for the option. If this is omitted, the value is taken from the text content of the option element.

text string Required.

Text for the option item.

selected boolean

Whether the option should be selected when the page loads. Takes precedence over the top-level value option.

disabled boolean

Sets the option item as disabled.

attributes object

HTML attributes (for example data attributes) to add to the option.

Options for label
Name Type Description
text string Required.

If html is set, this is not required. Text to use within the label. If html is provided, the text option will be ignored.

html string Required.

If text is set, this is not required. HTML to use within the label. If html is provided, the text option will be ignored.

for string

The value of the for attribute, the ID of the input the label is associated with.

isPageHeading boolean

Whether the label also acts as the heading for the page.

classes string

Classes to add to the label tag.

attributes object

HTML attributes (for example data attributes) to add to the label tag.

{% from "moduk/components/select/macro.njk" import modukSelect -%}

{{ modukSelect({
id: "sort",
name: "sort",
label: {
text: "Sort by"
},
items: [
{
value: "published",
text: "Recently published"
},
{
value: "updated",
text: "Recently updated",
selected: true
},
{
value: "views",
text: "Most views"
},
{
value: "comments",
text: "Most comments"
}
]
}) -}}

See GOV.UK select component guidance for:

  • when to use this component
  • when not to use it

How it works

See GOV.UK select component guidance for detail on how this component works.

Select with hint

You can add hint text to help the user understand the options and choose one of them.

This can be different to where you went before
<div class="govuk-form-group">
<label class="govuk-label" for="subject"> Choose location </label>
<div id="subject-hint" class="govuk-hint">
This can be different to where you went before
</div>
<select
class="govuk-select"
id="subject"
name="subject"
aria-describedby="subject-hint"
>

<option value="choose" selected>Choose location</option>
<option value="eastmidlands">East Midlands</option>
<option value="eastofengland">East of England</option>
<option value="london">London</option>
<option value="northeast">North East</option>
<option value="northwest">North West</option>
<option value="southeast">South East</option>
<option value="southwest">South West</option>
<option value="westmidlands">West Midlands</option>
<option value="yorkshire">Yorkshire and the Humber</option>
</select>
</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you’re using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
id string Required.

ID for each select box.

name string Required.

Name property for the select.

items array Required.

The items within the select component.

See items.

value string

Value for the option which should be selected. Use this as an alternative to setting the selected option on each individual item.

disabled boolean

If true, select box will be disabled. Use the disabled option on each individual item to only disable certain options.

describedBy string

One or more element IDs to add to the aria-describedby attribute, used to provide additional descriptive information for screenreader users.

label object Required.

The label used by the select component.

See label.

hint object

Can be used to add a hint to the select component.

See hint.

errorMessage object

Can be used to add an error message to the select component. The error message component will not display if you use a falsy value for errorMessage, for example false or null.

See error message.

formGroup object

Additional options for the form group containing the select component.

See formGroup.

classes string

Classes to add to the select.

attributes object

HTML attributes (for example data attributes) to add to the select.

Options for formGroup
Name Type Description
classes string

Classes to add to the form group (for example to show error state for the whole group).

attributes object

HTML attributes (for example data attributes) to add to the form group.

beforeInput object

Content to add before the select used by the select component.

See beforeInput.

afterInput object

Content to add after the select used by the select component.

See afterInput.

Options for afterInput
Name Type Description
text string Required.

Text to add after the select. If html is provided, the text option will be ignored.

html string Required.

HTML to add after the select. If html is provided, the text option will be ignored.

Options for beforeInput
Name Type Description
text string Required.

Text to add before the select. If html is provided, the text option will be ignored.

html string Required.

HTML to add before the select. If html is provided, the text option will be ignored.

Options for hint
Name Type Description
text string Required.

If html is set, this is not required. Text to use within the hint. If html is provided, the text option will be ignored.

html string Required.

If text is set, this is not required. HTML to use within the hint. If html is provided, the text option will be ignored.

id string

Optional ID attribute to add to the hint span tag.

classes string

Classes to add to the hint span tag.

attributes object

HTML attributes (for example data attributes) to add to the hint span tag.

Options for items
Name Type Description
value string

Value for the option. If this is omitted, the value is taken from the text content of the option element.

text string Required.

Text for the option item.

selected boolean

Whether the option should be selected when the page loads. Takes precedence over the top-level value option.

disabled boolean

Sets the option item as disabled.

attributes object

HTML attributes (for example data attributes) to add to the option.

Options for label
Name Type Description
text string Required.

If html is set, this is not required. Text to use within the label. If html is provided, the text option will be ignored.

html string Required.

If text is set, this is not required. HTML to use within the label. If html is provided, the text option will be ignored.

for string

The value of the for attribute, the ID of the input the label is associated with.

isPageHeading boolean

Whether the label also acts as the heading for the page.

classes string

Classes to add to the label tag.

attributes object

HTML attributes (for example data attributes) to add to the label tag.

{% from "moduk/components/select/macro.njk" import modukSelect -%}

{{ modukSelect({
id: "subject",
name: "subject",
label: {
text: "Choose location"
},
hint: {
text: "This can be different to where you went before"
},
items: [
{
value: "choose",
text: "Choose location",
selected: true
},
{
value: "eastmidlands",
text: "East Midlands"
},
{
value: "eastofengland",
text: "East of England"
},
{
value: "london",
text: "London"
},
{
value: "northeast",
text: "North East"
},
{
value: "northwest",
text: "North West"
},
{
value: "southeast",
text: "South East"
},
{
value: "southwest",
text: "South West"
},
{
value: "westmidlands",
text: "West Midlands"
},
{
value: "yorkshire",
text: "Yorkshire and the Humber"
}
]
}) -}}

Changes from the GOV.UK version

MOD.UK Design System components are closely based on GOV.UK Design System components.

For this component we’ve changed the font.

Have you tested this component?

Let us know how we could improve this component or share your user research findings.

Email the MOD.UK Design System team at design-system@digital.mod.uk

Need help?

Email the MOD.UK Design System team at design-system@digital.mod.uk if you have questions or feedback.