Checkboxes
<gcds-checkboxes>
Also called: checklist, check list.
Checkboxes provide a set of options for multiple responses.
-
GitHub -
Figma
Checkbox component preview
On this page
Coding and accessibility for checkboxes
Apply required attributes
For the checkboxes to function properly, always use the following attributes with gcds-checkboxes:
nameoptions
Use the options attribute with checkboxes
The options attribute takes an array of objects to render each checkbox. The following is an example of one of the objects:
{
'id'!: '',
'label'!: '',
'value'?: '',
'checked'?: '',
'hint'?: '',
}
Within the options attribute, always use the object's id and label properties for each checkbox. The rest of the properties are optional.
Use the value attribute
- The
valueattribute provides a quick reference to the values of checked checkboxes inside thegcds-checkboxescomponent. valueis formatted as an array of strings. Example:[“checkboxOneValue”, checkboxTwoValue”]
Handle error messages and validation
-
Use the
requiredattribute to activate the required validator. Validation will happen by default on theonBlurevent. A missing or invalid entry will prompt an inline error message with preset text. -
If you need to change the validation event, use the
validate-onattribute. Validation can happen on blur, when the element loses focus, or manually with thevalidate()method. -
Use the
requiredattribute for fields that must be filled in. This places "required" at the end of the label. -
Use the
error-messageattribute to include an error message text for all required inputs. Avoid using error messages for optional ones. -
For optional fields, avoid adding the
error-messageattribute. -
For an optional field that needs validation based on user input (like validating an email address format), opt to add custom validation with a custom error message.
For a group of checkboxes
- Use the
legendattribute when passing more than one object to theoptionsattribute.gcds-checkboxeswill not render if there is nolegenddefined with more than one checkbox. - Use the
hintanderror-messageattributes to add additional information to the fieldset built intogcds-checkboxes.
For a single checkbox
- Pass a single object to the
optionsattribute to render a single checkbox without a fieldset. - Use the
hintanderror-messageattributes to add additional information to the checkbox.
Hide the checkboxes legend or labels
- Visually hide the legend or label, while maintaining it for assistive technologies.
- Set the
hide-legendattribute to true to visually hide the legend for checkbox groups. - Set the
hide-labelattribute to true to visually hide the label for a single checkbox.
- Set the
- Avoid adding hint text when you’re hiding the legend or else provide an empty hint string.
In this section
legend
The legend attribute defines the legend for a group of checkboxes.
<gcds-checkboxes legend="Legend" name="checkexample" options='[{"id": "form-check-1", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check1"}, {"id": "form-check-2", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check2"}]'>
</gcds-checkboxes>
name
The name attribute identifies the checkboxes component within a form. It is used to reference the component after a form is submitted.
<gcds-checkboxes legend="Legend" name="checkexample" options='[{"id": "form-check-1", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check1"}, {"id": "form-check-2", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check2"}]'>
</gcds-checkboxes>
options
The options attribute defines the checkboxes to render by taking an array of objects, one per checkbox.
<gcds-checkboxes legend="Legend" name="checkexample" options='[{"id": "form-check-1", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check1"}, {"id": "form-check-2", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check2"}]'>
</gcds-checkboxes>
autofocus
The autofocus attribute controls whether the first checkbox will be focused automatically when the page loads. By default, it is set to false. When set to true, the checkbox is focused on render.
<gcds-checkboxes autofocus legend="Legend" name="checkexample" options='[{"id": "form-check-1", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check1"}, {"id": "form-check-2", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check2"}]'>
</gcds-checkboxes>
disabled
The disabled attribute controls whether the checkboxes are disabled or not. By default, it is set to false.
<gcds-checkboxes disabled legend="Legend" name="checkexample" options='[{"id": "form-check-1", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check1"}, {"id": "form-check-2", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check2"}]'>
</gcds-checkboxes>
error-message
The error-message attribute defines the error message to display for invalid checkboxes.
<gcds-checkboxes error-message="Error message" legend="Legend" name="checkexample" options='[{"id": "form-check-1", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check1"}, {"id": "form-check-2", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check2"}]'>
</gcds-checkboxes>
form
The form attribute specifies the ID of the form that the checkboxes belong to.
<gcds-checkboxes form="form-id" legend="Legend" name="checkexample" options='[{"id": "form-check-1", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check1"}, {"id": "form-check-2", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check2"}]'>
</gcds-checkboxes>
hide-label
The hide-label attribute controls whether the label is hidden or not.
<gcds-checkboxes hide-label name="checkbox" options='[{ "label": "Label for checkbox 1", "id": "checkbox1", "value": "checkbox1"}]'>
</gcds-checkboxes>
hide-legend
The hide-legend attribute controls whether the legend is hidden or not.
<gcds-checkboxes hide-legend legend="Legend" name="checkexample" options='[{"id": "form-check-1", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check1"}, {"id": "form-check-2", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check2"}]'>
</gcds-checkboxes>
hint
The hint attribute defines the hint text displayed under the label or legend.
<gcds-checkboxes hint="Hint / Example message." legend="Legend" name="checkexample" options='[{"id": "form-check-1", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check1"}, {"id": "form-check-2", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check2"}]'>
</gcds-checkboxes>
required
The required attribute controls whether the checkbox fieldset is required or not. When set to true, it adds “required” after the label or legend, and activates the required validator.
<gcds-checkboxes required legend="Legend" name="checkexample" options='[{"id": "form-check-1", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check1"}, {"id": "form-check-2", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check2"}]'>
</gcds-checkboxes>
validate-on
The validate-on attribute defines the validation event for the checkboxes.
<gcds-checkboxes validate-on="other" required legend="Legend" name="checkexample" options='[{"id": "form-check-1", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check1"}, {"id": "form-check-2", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check2"}]'>
</gcds-checkboxes>
value
The value attribute contains an array of the values of all currently checked checkboxes. It updates as selections change.
<gcds-checkboxes value='["check2"]' legend="Legend" name="checkexample" options='[{"id": "form-check-1", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check1"}, {"id": "form-check-2", "label": "Label", "hint": "Description or example to make the option clearer.", "value": "check2"}]'>
</gcds-checkboxes>
Code builder
Generate an instance of the component you need by selecting its code properties.
-
Explore by choosing different code values to generate the instance you want.
-
Get the code and pull it into your environment.
-
Add any copy you need to the component (like text for a label).
Note: The code builder uses English for all code elements, which follows standard practice.
Help us improve
Have questions or a request? Give feedback on our contact form.
Something's wrong? Raise it through GitHub with an