Form elements - Select

Use select to let users choose an option from a list.

Open this default select example in new window
Copy default select code
<div class="hs2-form-group">
  <label class="hs2-label" for="select-1">
    Label text goes here
  </label>
  <select class="hs2-select" id="select-1" name="select-1">
    <option value="1">Option 1</option>
    <option value="2" selected>Option 2</option>
    <option value="3" disabled>Option 3 (disabled)</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": "Option 1"
    },
    {
      "value": 2,
      "text": "Option 2",
      "selected": true
    },
    {
      "value": 3,
      "text": "Option 3 (disabled)",
      "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.