Sub topic 8
Monthly and annual billing toggles
This chapter covers the control that switches a pricing page between monthly and annual prices. It assumes you can write HTML, CSS, and basic JavaScript. It also assumes you have read the earlier chapters on tier structure and on the anatomy of a comparison table. The toggle changes the numbers in that table, so the table needs to be clear first.
What the toggle is for
Annual billing helps the seller. It brings in cash up front and lowers churn, because a customer who paid for a year rarely cancels in month three. Customers benefit when the discount is real. Most also want to know what leaves their card and when.
A toggle lets one page answer both questions. It does not say which option to choose. Its job is to show accurate prices for each period and to keep the page consistent while the visitor switches.
One source of truth for prices
The most common bug is a toggle and a stated discount that disagree. A badge says "Save 20%" while the numbers show 17%. This happens when prices are typed by hand into the markup, the badge, and the checkout link.
Prevent it by keeping each plan's prices in one data structure and computing every displayed figure from it.
type BillingPeriod = "monthly" | "annual";
interface Plan {
id: string;
name: string;
monthlyCents: number;
annualPerMonthCents: number;
}
const formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
function displayPrice(plan: Plan, period: BillingPeriod): string {
const cents =
period === "annual" ? plan.annualPerMonthCents : plan.monthlyCents;
return formatter.format(cents / 100);
}
function annualTotal(plan: Plan): string {
return formatter.format((plan.annualPerMonthCents * 12) / 100);
}
function annualSavingsPercent(plan: Plan): number {
return Math.round((1 - plan.annualPerMonthCents / plan.monthlyCents) * 100);
}
const team: Plan = {
id: "team",
name: "Team",
monthlyCents: 1500,
annualPerMonthCents: 1200,
};
console.log(displayPrice(team, "annual")); // "$12.00"
console.log(annualTotal(team)); // "$144.00"
console.log(annualSavingsPercent(team)); // 20Three details in this code matter:
- Integer cents. Storing money as integers avoids floating-point surprises such as 0.1 + 0.2.
Intl.NumberFormat. It handles currency symbols, separators, and locale rules. Do not concatenate"$"and a number yourself.- A separate
annualTotal. The annual plan is usually shown as a per-month price, but the card is charged once for the whole year. That total must be on the page.
If you generate the page on a server, run these functions at build or render time and write the results into the markup. The browser then only chooses which precomputed string to show.
Showing annual prices honestly
A per-month figure under an annual plan can mislead. A visitor who sees "$12" may assume a monthly charge. Follow these rules:
- Label the price with its unit, such as "per user per month".
- Under the annual price, state the billing event: "$144.00 billed once a year".
- Under the monthly price, say "Billed monthly" so the two states have the same structure.
- If you show the savings, derive the percentage from the data, as in the code above.
- Show the original monthly price struck through only if customers actually pay it. Otherwise it is a fake reference price, which the transparency chapter treats as a dark pattern.
Taxes need one more decision. If you add VAT or sales tax at checkout, say so near the table ("Prices exclude tax"). The toggle should not change that message.
Building an accessible toggle
The spine's markup uses a button with aria-pressed. A native <button> is focusable and responds to Enter and Space without extra code. The attribute aria-pressed tells assistive technology whether the button is on or off.
<button type="button" id="billing-toggle" aria-pressed="false" hidden>
Bill annually
</button>
<p id="billing-status" class="visually-hidden" role="status"></p>
<p>
<span data-monthly="$15.00" data-annual="$12.00">$15.00</span>
per user per month
</p>
<p data-monthly="Billed monthly" data-annual="$144.00 billed once a year">
Billed monthly
</p>
<a href="/signup?plan=team&billing=monthly" data-plan="team">Choose Team</a>The button label stays "Bill annually" in both states. If you change the label when the state changes, a screen reader announces something like "Bill monthly, pressed", which contradicts itself. The state belongs in aria-pressed, and the label names the option.
The button starts with hidden and is shown by script. Without JavaScript, the page shows monthly prices, which are accurate and complete, and the dead control never appears.
The role="status" paragraph is a polite live region. When script writes "Showing annual prices." into it, screen readers announce the change once. Do not make every price a live region, because the reader would hear a dozen announcements per click. The visually-hidden class is the one from the earlier table chapter.
The script
const toggle = document.querySelector("#billing-toggle");
const status = document.querySelector("#billing-status");
function setBilling(annual) {
toggle.setAttribute("aria-pressed", String(annual));
document.querySelectorAll("[data-monthly][data-annual]").forEach((el) => {
el.textContent = annual ? el.dataset.annual : el.dataset.monthly;
});
document.querySelectorAll("a[data-plan]").forEach((link) => {
const url = new URL(link.getAttribute("href"), location.href);
url.searchParams.set("billing", annual ? "annual" : "monthly");
link.setAttribute("href", url.pathname + url.search);
});
status.textContent = annual
? "Showing annual prices."
: "Showing monthly prices.";
}
toggle.hidden = false;
toggle.addEventListener("click", () => {
setBilling(toggle.getAttribute("aria-pressed") !== "true");
});
const params = new URLSearchParams(location.search);
setBilling(params.get("billing") === "annual");This version goes beyond the spine's minimal example in two ways:
- The call-to-action links follow the toggle. If a visitor picks annual and clicks "Choose Team", checkout must open with annual billing. A mismatch there is the worst outcome: the page promised $144 per year and checkout says $15 per month.
- The state is read from the URL. A link such as
/pricing?billing=annualopens the page in annual mode. Sales, email campaigns, and support can then share exact views, and a page reload does not reset the choice.
The script runs the update once at load, so the initial markup and the toggle state cannot drift apart.
Choosing the default
The default state is a design decision with ethical weight. There are two common choices:
| Default | Effect | Risk |
|---|---|---|
| Monthly | Shows the highest headline price, which is the one that applies to a visitor who has not yet committed | Lower apparent value. The annual saving is visible only after a click |
| Annual | Shows the lower per-month number first and leads visitors toward the longer commitment | Visitors may not notice they are choosing a year-long charge |
Annual by default is acceptable when the page states "billed once a year" next to every price and checkout repeats the total. It becomes a dark pattern when the toggle is hard to see or the yearly charge is revealed only at payment. A good test is to ask whether a visitor who skims the page can say what they will be charged today.
Common errors
- Layout shift. "$12.00" and "$1,200.00" differ in width, so the table jumps when toggled. Give price elements a
min-widthor use tabular numerals withfont-variant-numeric: tabular-nums. - Plans without an annual option. A free plan has no annual price, and an enterprise plan may say "Contact us". Keep those cells identical in both states instead of showing "$0 billed once a year".
- Hiding the toggle on mobile. In the responsive table, keep the toggle above the table and outside the scroll container so it stays reachable.
- Using a checkbox with extra styling. A custom switch built from divs has no keyboard support or state for assistive technology. Use a real button or a native input.
- Rounding mismatches. If $12.50 per month rounds to $13 in the display but checkout charges $150, the numbers disagree. Show cents when the price has them.
If you want a clearer control, use two radio buttons labeled "Monthly" and "Annual" inside a fieldset. A visitor sees both options at once instead of inferring the other state from one button. It takes more space, but it removes any doubt about which period is active.
Exercise
A visitor selects annual billing, then clicks "Choose Team". Name two things on the page that must reflect the annual choice for the experience to be consistent, and explain why the button label should not change from "Bill annually" to "Bill monthly".
Show answer
The displayed prices and the checkout link (its billing parameter) must both reflect annual billing, along with the "billed once a year" total. The label should stay constant because aria-pressed already carries the on or off state. Changing the label as well makes a screen reader announce a contradiction such as "Bill monthly, pressed".
Exercise
A plan has monthlyCents of 2000 and annualPerMonthCents of 1700. What does annualSavingsPercent return, and what total should the page show for annual billing?
Show answer
It returns 15, because 1 - 1700/2000 = 0.15. The annual total is 1700 * 12 = 20400 cents, shown as $204.00 billed once a year.