Opens in a new tab

Toggle Switch

Widgets4 minutes readUpdated 6 Oct 2026

Turns an element into a real switch (role="switch" with aria-checked). Every change writes an attribute on a target, by default data-billing="yearly" or "monthly" on the closest section, so the content that matches can show.

Toggle Switch is a Free widget: it works as soon as the companion plugin is installed.

Requires: nothing. Plain JavaScript, no library.

Where to find it#

HowWhere
As an elementElements panel > Bricksify > Toggle Switch: a Block inserted with a Monthly label, a track with its thumb and an Annual label.
As a control groupA Toggle Switch (Bricksify) group on Block, Div and Button: set Behaviour to On / off switch.

Both use the same settings, saved under the same names. See How widgets work for the difference between the two.

Settings#

SettingOptions and defaultWhat it does
BehaviourNone / On / off switch / Segmented control / Value stack / Meter (the element is a switch unless set to Segmented control)Turns the switch on. Click, tap, Space and Enter flip it.
On at loadOffStarts switched on: aria-checked="true" and the “on” value written on load.
Attribute written on the targetdata-billingAny attribute name except event handlers (on…).
Value when on / offyearly / monthlyThe two values written.
Target (CSS selector)The closest sectionThe closest ancestor that matches, else the first match on the page.
Accessible nameThe text insideWhat a screen reader announces, for example “Annual billing”.

Show the matching content#

Give the prices for each state a class, for example bx-when-monthly and bx-when-yearly, and hide the ones that do not match from your section class: [data-billing="yearly"] .bx-when-monthly, :not([data-billing="yearly"]) .bx-when-yearly {display: none}. Set the starting value as an attribute on the section too, so the page is right before JavaScript runs.

Style the on state#

The switch carries aria-checked="true" when on. Target it from your class, for example .my-switch[aria-checked="true"] .my-thumb {transform: translateX(14px)}. The inserted element has a 32 by 18px track and a 16px thumb with a 0.15s transition; Bricks controls style everything else.

Keyboard and screen readers#

A Block or Div gets tabindex="0"; a Button is focusable already. The focus ring is a 2px outline in the text colour, offset 4px. Every change also fires a bricksify:switch event with detail.checked, for your own scripts.

Segmented control, value stacks and meters#

Besides the on / off switch, Behaviour offers three more options on Block and Div (the Toggle Switch element offers the segmented control too). They work together inside one section: the control writes its value on the section, every value stack and meter inside follows it.

BehaviourSettingsWhat it does
Segmented controlValues (comma separated), Chosen at load (option number), Attribute, Target, Accessible name; pill colour, border colour, radius, shadow, chosen text colour, slide duration (300ms)Every child is one option (use buttons). They become tabs (role="tab", aria-selected, roving tabindex): Arrow keys, Home and End move and choose. A pill measured to the chosen option slides under it (0.3s, ease-out) and follows resizes. The chosen value is written on the target, e.g. data-billing="annual", plus data-sw-option with its number.
Value stackChildren: show only the chosen one / show all and mark it; Stack name; Count to the new number + duration (450ms); Count up from 0 (on load / in view); Announce changesChild 1 belongs to option 1, child 2 to option 2 (for the on / off switch: off, on). Only the matching child shows, or with “mark” the matching child gets the class is-chosen. Counting keeps prefix, suffix and thousands separators ($1,176). A stack with a single child and Count up from 0 is a simple count-up.
MeterReads the stack named, Number at full width (100), Fill duration (550ms)Its width becomes the number shown by that stack divided by the full-width number, e.g. savings 25 of 38 = 65.79%. Give it that width as its own Width too, for visitors without JavaScript.

Before the script runs the chosen option and the matching stack children already show (CSS reads Chosen at load), so nothing jumps. With reduced motion the numbers change at once and the pill and meter jump. See it in Pricing Commitment Term (segmented control, six stacks, a meter and marked ticks) and Pricing Radar Plans (count-up prices).

See it live#

Open the demo page.