Fieldset
Use this component to wrap multiple form input components and provide context to the contained inputs with a title, using the <legend>
element.
How to use this component
Usually, the onsFieldset
macro is already called from within form input components, so you should not need to call this yourself.
However if you need to create a pattern or component that requires a field you can use Nunjucks' call functionality (opens in a new tab)
Legend as page/question title
To avoid the legend repeating the <h1>
on a question page, you can add the utility class .ons-u-vh
to visually hide the legend.
However, this does not avoid repetition for screen reader users. Therefore, you must wrap the <h1>
in the legend. There are two ways of doing this:
With the question macro
- When using the question macro include the parameter
"legendIsQuestionTitle": true
inside theonsQuestion
configuration. - Include
"dontWrap": true
in the configuration of the input you are using. - Do not use the
legend
andlegendClasses
parameters on the input being used.
Without the question macro
- Only include the parameter
"legendIsQuestionTitle": true
in the configuration of the input you are using. - Do not use the
legendClasses
parameters.
Configuration
Name | Type | Required | Description |
---|---|---|---|
id | string | false | The HTML id of the fieldset |
classes | string | false | Classes to apply to the fieldset |
legend | string | true (unless legendIsQuestionTitle is set) | Text for the fieldset’s legend |
legendClasses | string | false | Classes to apply to the legend element |
legendIsQuestionTitle | boolean | true (unless legend is set) | Creates an h1 inside the legend . Use when there is only a single fieldset on the page. |
legendTitleClasses | string | false | Classes to apply to the h1 created using legendIsQuestionTitle |
description | string | false | Description for the fieldset |
attributes | object | false | HTML attributes (for example, data attributes) to add to the fieldset |
error | Error (ref) | false | Configuration for validation errors |
dontWrap | boolean | false | Prevents the fields from being wrapped in a fieldset |
Help improve this page
Let us know how we could improve this page, or share your user research findings. Discuss the ‘Fieldset’ component on GitHub (opens in a new tab)