Form elements - Select

Use select to let users choose an option from a long list but only use it as a last resort.

Open this default select example in new window
Copy default select code
<div class="nhsuk-form-group">
  <label class="nhsuk-label" for="select-1">
    Label text goes here
  </label>
  <select class="nhsuk-select" id="select-1" name="select-1">
    <option value="1">NHS.UK frontend option 1</option>
    <option value="2" selected>NHS.UK frontend option 2</option>
    <option value="3" disabled>NHS.UK frontend option 3</option>
  </select>
</div>
Close default select code
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.

Nunjucks arguments for default select
Name Type Required Description
Name id Type string Required true Description Id for each select box.
Name name Type string Required true Description Name property for the select.
Name items Type array Required true Description Array of option items for the select.
Name items[].value Type string Required false Description Value for the option item. Defaults to an empty string.
Name items[].text Type string Required true Description Text for the option item.
Name items[].selected Type boolean Required false Description Sets the option as the selected.
Name items[].disabled Type boolean Required false Description Sets the option item as disabled.
Name items[].attributes Type object Required false Description HTML attributes (for example data attributes) to add to the option.
Name label Type object Required false Description Label text or HTML by specifying value for either text or html keys.
Name hint Type object Required false Description Options for the hint component.
Name errorMessage Type object Required false Description Options for the error message component. The error message component will not display if you use a falsy value for `errorMessage`, for example `false` or `null`.
Name classes Type string Required false Description Classes to add to the select.
Name attributes Type object Required false Description HTML attributes (for example data attributes) to add to the select.
Copy default select code
{% from 'select/macro.njk' import select %}

{{ select({
  "id": "select-1",
  "name": "select-1",
  "label": {
    "text": "Label text goes here"
  },
  "items": [
    {
      "value": 1,
      "text": "NHS.UK frontend option 1"
    },
    {
      "value": 2,
      "text": "NHS.UK frontend option 2",
      "selected": true
    },
    {
      "value": 3,
      "text": "NHS.UK frontend option 3",
      "disabled": true
    }
  ]
}) }}
Close default select code

How to use select

The select component allows users to choose an option from a long list. Before using it, try other questions which will allow you to present users with fewer options. Consider using a different solution, such as radios.

Research

Watch a video about how some users struggle with selects: Alice Barlett: Burn your select tags.

Have you tested this component?

If so, please share your research findings and let us know how it has worked for you. This will help us improve it for everyone.

Before you start, you will need a GitHub account. It's an open forum where we collect feedback.

If you have any questions, you can message us on Slack. You will need a Slack account if you do not have one. Or you can contact us by email.

Updated: January 2019