data-select-all

Master checkbox for bulk selection. Add data-select-all to a checkbox to control a group of related checkboxes with indeterminate state and count display.

Overview

The data-select-all attribute enhances a checkbox to act as a master toggle for a group of target checkboxes. When toggled, all targets check or uncheck together. When individual targets are changed, the master reflects an indeterminate state if some (but not all) are selected.

How It Works

  1. Add data-select-all to a checkbox, with the value set to a CSS selector matching the target checkboxes
  2. The init script scopes targets to the nearest <table>, <form>, <fieldset>, or [data-select-all-scope] ancestor — falling back to document
  3. Toggling the master checks/unchecks all targets and dispatches change events on each
  4. Toggling any individual target syncs the master: all checked = checked, some checked = indeterminate, none checked = unchecked
  5. A select-all:change custom event is dispatched on every state change

Attributes

Attribute Type Description
data-select-all string CSS selector for the target checkboxes (e.g., .row-select).
data-selected-count boolean Place on a separate element to display the number of selected items. Updated automatically.
data-select-all-scope boolean Place on an ancestor element to scope the master/target relationship. Useful when the same target selector appears in multiple groups.
data-select-all-init boolean Set automatically to prevent double-binding. Do not set manually.

Selected Count Display

Add data-selected-count to any element within the same scope to show a live count of selected items.

The count element's textContent is updated on every state change, including the initial sync.

Checkbox List

Works with any checkbox group, not just tables. Use a <fieldset> with data-select-all-scope to scope the group:

Scoping

The master checkbox automatically scopes its target lookup to the nearest ancestor that is a <table>, <form>, <fieldset>, or an element with [data-select-all-scope]. This allows multiple independent select-all groups on the same page, even with the same CSS selector for targets.

Events

The master checkbox dispatches a select-all:change event on every state change (master toggle or individual target change).

Event Detail Description
select-all:change { checked: number, total: number, selected: Element[] } Fired on every state change. checked is the count of selected targets, total is the total target count, and selected is an array of checked target elements.

Dynamic Elements

Select-all checkboxes added to the DOM after page load are automatically enhanced via a MutationObserver. No manual initialization is needed.