• Molecule
  • CORE COMPONENT
  • Taxonomic name: M-CHECKBOX-GROUP--MULTISELECT
  • Extension: FORMS-EXTENDED-A
  • Added on: v3.0.0 (15/09/17)
  • Updated on: v5.9.0 (13/07/21)

Checkbox multiselect

Checkbox multiselects adapt a standard checkbox group to become a dropdown list. They allow users to select from a larger list of checkboxes and are preferable to a a standard group of checkboxes in these instances, as they keep pages shorter. Where there are more than five options available at desktop, or three at mobile, multiselects will automatically add a scrolling facility.


Design and usage

Checkboxes allow users to select none, one or more items from a list of options.

The default version of this component pushes down the content on the page when it is opened, but an additional variant is available which will allow the open component to overlay other content on the page instead - see 'overlay variant' example below.

  • Checkboxes should behave independently from each other.
  • A single checkbox should be used where a user needs to indicate agreement (for example to terms and conditions).
  • The checkbox itself, and its container are clickable, to provide a larger target area.
  • Checkbox labels may wrap to a maximum of two lines.
  • Standard multiselects which push down down can show/hide content or form elements.
  • Aria-invalid must be used to indicate when an error has occurred with this element.
  • Aria-describedby must be used to link any help, instructional and/or error text with the element/form fields.
  • Do not use for mutually exclusive options - use segmented controls or drop down lists instead.
  • Do not use a negative statement for a checkbox label.
  • Overlay variant multiselects must not show/hide content or form elements; if used incorrectly, content/form elements would have to either populate under the overlay or after the overlay, both of which would present confusing behaviour.

For documentation on standard checkboxes see our checkbox documentation.

Examples

Standard checkbox multiselect

Interactive example

Code example

<fieldset class="m-form-row">
	<legend class="a-label">...</legend>
	<div class="m-form-row__content">
		<ul class="m-form-row-group m-form-row-group--option-group" data-module="m-multiselect">
			<li class="m-form-row-group-item m-form-row-group-item--full-width">
				<label class="a-checkbox" for="...">
					<input class="a-checkbox__input" type="checkbox" name="..." id="..." value="..." />
					<span class="a-checkbox__label"><span class="a-checkbox__label-inner">...</span></span>
					<span class="a-checkbox__ui"></span>
				</label>
			</li>
			...
		</ul>
	</div>
</fieldset>

Standard checkbox multiselect with scrollbar

Standard checkbox multiselect - overlay variant

Interactive example

Code example

<fieldset class="m-form-row">
	<legend class="a-label">...</legend>
	<div class="m-form-row__content">
		<ul class="m-form-row-group m-form-row-group--option-group" data-module="m-multiselect"  data-variant="m-multiselect--overlay">
			<li class="m-form-row-group-item m-form-row-group-item--full-width">
				<label class="a-checkbox" for="...">
					<input class="a-checkbox__input" type="checkbox" name="..." id="..." value="..." />
					<span class="a-checkbox__label"><span class="a-checkbox__label-inner">...</span></span>
					<span class="a-checkbox__ui"></span>
				</label>
			</li>
			...
		</ul>
	</div>
</fieldset>

Standard checkbox multiselect with scrollbar - overlay variant

Valid checkbox multiselect

Interactive example

Code example

<fieldset class="m-form-row is-valid">
	<legend class="a-label">...</legend>
	<div class="m-form-row__content">
		<p class="m-form-row__validation-message">...</p>
		<ul class="m-form-row-group m-form-row-group--option-group" data-module="m-multiselect">
			<li class="m-form-row-group-item m-form-row-group-item--full-width">
				<label class="a-checkbox" for="...">
					<input class="a-checkbox__input" type="checkbox" name="..." id="..." value="..." />
					<span class="a-checkbox__label"><span class="a-checkbox__label-inner">...</span></span>
					<span class="a-checkbox__ui"></span>
				</label>
			</li>
			...
		</ul>
	</div>
</fieldset>

Errored checkbox multiselect

Interactive example

Code example

<fieldset class="m-form-row is-error" aria-invalid="true" aria-describedby="[ row error id ]">
	<legend class="a-label">...</legend>
	<div class="m-form-row__content">
		<p class="m-form-row__error-message" id="..."><span class="u-hidden--visually">Error: </span>...</p>
		<ul class="m-form-row-group m-form-row-group--option-group" data-module="m-multiselect">
			<li class="m-form-row-group-item m-form-row-group-item--full-width">
				<label class="a-checkbox" for="...">
					<input class="a-checkbox__input" type="checkbox" name="..." id="..." value="..." />
					<span class="a-checkbox__label"><span class="a-checkbox__label-inner">...</span></span>
					<span class="a-checkbox__ui"></span>
				</label>
			</li>
			...
		</ul>
	</div>
</fieldset>

Disabled checkbox multiselect

Interactive example

Code example

<fieldset class="m-form-row is-disabled" disabled>
	<legend class="a-label">...</legend>
	<div class="m-form-row__content">
		<ul class="m-form-row-group m-form-row-group--option-group" data-module="m-multiselect">
			<li class="m-form-row-group-item m-form-row-group-item--full-width">
				<label class="a-checkbox" for="...">
					<input class="a-checkbox__input" type="checkbox" name="..." id="..." value="..." />
					<span class="a-checkbox__label"><span class="a-checkbox__label-inner">...</span></span>
					<span class="a-checkbox__ui"></span>
				</label>
			</li>
			...
		</ul>
	</div>
</fieldset>

Required checkbox multiselect

<fieldset class="m-form-row" aria-required="true">
	<legend class="a-label">...</legend>
	<div class="m-form-row__content">
		<ul class="m-form-row-group m-form-row-group--option-group" data-module="m-multiselect">
			...
		</ul>
	</div>
</fieldset>

Development and test

Notes for developers

Checkbox multiselects add their functionality with the simple addition of a data-module="m-multiselect" tag within the m-form-row-group m-form-row-group--option-group ul.

When creating a group of checkboxes they must be contained within their one fieldset where the legend is the question for the group of checkboxes.

Within a group of checkboxes the name attribute must be unique to the form, and the id attribute must be unique to the page.

When disabling form elements, do not solely rely on the is-disabled class, but ensure that the disabled attribute is also set on the relevant form element or input. If an entire set of form elements is to be disabled, the is-disabled class and disabled attribute should be added directly to the fieldset instead, and not to each individual element within it. However if only one element from the set is disabled, then the class and attribute should be applied to that element only.

If the native form validation needs to be disabled set the novalidate attribute on the form.

Aria usage for errors and help text

To aid screen reader users in completing forms without error and also with understanding and fixing any errors that have occured, some additional aria attributes are now recommended to be applied across all form fields/rows.

All form row help, explanatory text, form row instructions and form row errors must have a unique id assigned to them and be appropriately linked to either the individual form input or the form row using aria-describedby. This ensures that the help text is read out immediately on focus of the form input/fieldset. When there is more that one item that requires referencing within the aria-describedby attribute the id values must be supplied in the form of a space separated list. For example if there is an error with id of 'err1' and help text with id of 'help1' the value for the aria-describedby attribute would be 'err1 help1'.

All inline form error messages should start with the hidden text 'Error: ' so that when read by out by a screen reader it is immediately clear that the text being read is an error message.

All erroring form fields (or fieldset if a group or radios/checkboxes) must have aria-invalid="true" set on them, ensuring that when a user focus' on an errored form field, the field will be announced as invalid, so the user is then aware that there in an issue that requires fixing.

When linking help or errors, or even adding aria-invalid to the markup careful consideration must be taken as to where it is most appropriate to apply the value. The following rules should be used to decide where the attribute(s) should be applied.

  • Single form field - All attributes should be applied directly to that form field
  • Group of form fields but only one in error or help text applies to only one - Apply the attribute(s) to the only field in error or where the help text applies
  • Group of form field where all in error or the help text applies to all - Apply the attribute(s) to the groups surrounding fieldset (form row)

There may situations where more than one of these rules applies to the overall form row for example a date range where the instruction text applies to both fields so should be linked to the fieldset for the date range but one of the two fields is in error so the error should be linked to just that field not to the fieldset.

Important messages

To help those using assistive technologies, make sure that success/error messages which need to be announced to users have their aria attributes updated to role="alert" and/or aria-live="assertive".

Overlay variant

An additional variant is available for this component which allows it to overlay other content on the page (rather than pushing it down). This can be activated simply by adding an additional data tag - data-variant="m-multiselect--overlay" - alongside the data-module. If the component is placed at the bottom of the page, adding this class will extend the page height to allow room for the overlaid element, which may force white space below the footer; therefore, please check the outcome before finalising this use.

Using autocomplete

Users appreciate when websites save them time by automatically filling common fields like names, email addresses and other frequently used fields, plus it helps to reduce potential input errors, especially on virtual keyboards and small devices.

Browsers use many methods to determine which fields they can auto-populate based on previously specified data by the user, and you can give hints to the browser by providing both the name attribute and the autocomplete attribute on each input element.

Further details can be found in the developer notes on the form introduction page.

Controling the checkbox multiselect component programatically

Checkbox multiselect can be controlled programatically to select/deselect all, select/deselect a selection of checkboxes and close the multiselect. The following methods are available for use:

Method Description
select( [ '#inputId1', '#inputId2' ] ) Select one or more checkboxes using an array of ids.
selectAll() Select all checkboxes.
deselect( [ '#inputId1', '#inputId2' ] ) Deselect one or more checkboxes using an array of ids.
deselectAll() Deselect all checkboxes.
close() Close the multiselect if it is currently open.

For example:

document.addEventListener( 'frameworkready', function () {
	FRAMEWORK.require([ 'blocks/m-multiselect' ], function ( mMultiselectModule ) {
		var mMultiselectEl = document.querySelector( '#multiselectId' ),
			mMultiselectInstance = mMultiselectModule.initInstance( mMultiselectEl );		mMultiselectInstance.select( [ '#inputId1', '#inputId2' ] );
	});
});

Changing locale settings

Default text for the checkbox multiselect ('Please select..', 'All selected' etc) can be changed for a particular locale.

en: {
	mCheckboxGroupMultiselect : {
		pleaseSelect : 'Please select...',
		allSelected : 'All selected',
		selected : ' selected',
		selectAll : 'Select all',
	},
},

For more information on this, see our page on how to change locale settings within JavaScript.

The locale settings can also be changed on an individual basis if required. This is achieved by including any of the following data attributes:

  • data-pleaseselect
  • data-allselected
  • data-selected
  • data-selectall

Extension component

This component forms part of the 'forms-extended-a' extension and so requires an additional stylesheet to be loaded in order to be used; include the following code in the header of your page to attach the relevant additional stylesheet:

<link media="all" href="./assets/[ theme type ]/[ release version ]/[ organisation ]/[ theme name ]/css/forms-extended-a.css" rel="stylesheet" />

Notes for testers

  • In this instance the label for the checkbox is the wording directly on the right of the checkbox. Clicking/pressing the explanatory text for the group of checkboxes will not transfer focus to any of the checkboxes like it would for a text input.
  • The label should be displayed to the right of the checkbox and should activate the checkbox when selected (as should the checkbox itself).
  • It should be really clear which label applies to which checkbox by using background shading to group the label and the checkbox together.
  • Check for additional white space forced below the footer if the overlay variant is used, and confirm result with developers.

Classes overview

The following table gives you a quick overview of the CSS classes that can be applied.

Class Outcome Required Applied to Comments
.m-form-row-group Base styling for a group of form inputs Yes .m-form-row__content > ul  
.m-form-row-group--option-group Base styling for a group of checkboxes Yes .m-form-row-group  
.m-form-row-group-item Base for individual checkboxes Yes .m-form-row-group > li  
.m-form-row-group-item--full-width Modifer to force radios to be full width (not natural width) when not in column system &bnsp; .m-form-row-group-item Only exclude if using a group of standalone checkboxes
.a-checkbox Additional styles for checkboxes Yes span  
.a-checkbox__input Additional styles for checkboxes Yes .a-checkbox > input  
.a-checkbox__label Styles for the label Yes .a-checkbox__input + span  
.a-checkbox__label-inner Styles in order to keep the label centered when multiple lines are needed Yes .a-checkbox__label > span  
.a-checkbox__ui Animations for the checkbox Yes .a-checkbox__label + span  

Keyboard operations

TAB

Tabbing to an checkbox input should make the input clearly visually different so that the focus point on the page is obvious to the user.

CURSOR KEYS

With focus on the checkbox, pressing SPACE will select/de-select the checkbox.

Component releases

  • Added on: v3.0.0 (15/09/17)
  • Updated on: v5.9.0 (13/07/21)

Latest update:

  • updated: Checkbox multiselect can now be controlled programatically to select/deselect all, select/deselect a selection of checkboxes and close the multiselect.

Full version history

A full history of changes and enhancements detailing all minor and major updates to the component.

View full version history

Want something new in Framework, or to chat about an issue you're having with it?

Contact the team