Sub topic 8
Monthly and annual billing toggles
This chapter assumes you can write basic HTML, CSS and JavaScript, and that you know what a subscription plan is. It covers how to build a toggle that switches the displayed prices between monthly and annual billing, and how to keep the prices it shows accurate.
What the toggle does
Most subscription products offer two billing periods. Monthly billing is flexible. Annual billing usually costs less per month in exchange for a longer commitment. A toggle lets the visitor choose a period once, and every plan card or table column then updates.
The toggle changes the numbers on the page. It does not change the structure of the plans. If switching periods also changes which features a plan includes, visitors will compare the wrong things.
Keep one source of truth
The most common error is typing prices into the HTML by hand, in two places, and then editing only one. The toggle then shows a price that disagrees with the discount you advertise ("Save 20%").
Store plan data in one structure. Compute the displayed price and the savings from that data.
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 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(annualSavingsPercent(team)); // 20Prices are stored as integer cents, which avoids floating-point rounding errors. Intl.NumberFormat handles the currency symbol and separators.
Building the toggle
A toggle is a button. Use aria-pressed so assistive technology announces whether annual billing is currently on.
const toggle = document.querySelector("#billing-toggle");
toggle.addEventListener("click", () => {
const annual = toggle.getAttribute("aria-pressed") !== "true";
toggle.setAttribute("aria-pressed", String(annual));
document.querySelectorAll("[data-monthly][data-annual]").forEach((el) => {
el.textContent = annual ? el.dataset.annual : el.dataset.monthly;
});
});Each price element carries both strings as data attributes, for example <span data-monthly="$15.00" data-annual="$12.00">$15.00</span>. In a real page, generate these attributes from the plan data above, on the server or at build time, so they cannot drift.
The button's label should stay the same, such as "Bill annually". The aria-pressed state carries the on or off meaning. Changing the label as well makes screen readers announce a confusing double change.
Common errors
- Hiding the real charge. If annual billing charges the full year at once, say so near the price, for example "$12 per month, billed $144 yearly". Showing only the monthly figure misleads visitors.
- Defaulting to annual without saying so. The default state should be obvious. Either show the period in plain text next to every price or make the active choice visually clear.
- Updating only some prices. Check the plan cards, the comparison table, and any call-to-action text such as "Start Team annual".
- Relying on color alone. Show the on state with a label or a position change as well as color.
- Rounding the discount. If the real saving is 17.5%, do not advertise "20%". Compute it from the data.
- No script fallback. Render monthly prices in the HTML, so the page still shows correct numbers if JavaScript fails.
Placement and wording
Put the toggle directly above the plans, where the prices it affects are visible without scrolling. Label both options clearly, such as "Monthly" and "Annual", and show the saving next to the annual option, such as "Save 20%". The saving text should come from annualSavingsPercent, so it always matches the prices.
Exercise
A plan costs 2000 cents per month on monthly billing and 1600 cents per month on annual billing. What savings percentage should the page show, and why is it better to compute it than to type it by hand?
Show answer
The saving is (1 - 1600/2000) × 100 = 20%. Computing it from the stored prices means the advertised discount and the displayed prices cannot disagree if one of them changes later.
Exercise
Why should the toggle button keep the same label while only its aria-pressed value changes?
Show answer
The aria-pressed attribute already tells assistive technology whether the option is on or off. Changing the label too makes the state change get announced twice and can leave the meaning unclear.