How to Build a Persistent 3-Step Checklist Widget in 12 Lines of Vanilla JS (No Frameworks)
Most interactive checklists on the web pull in 150 KB of React state libraries just to toggle three checkboxes. When I built a live countdown & 3-step Cyber Monday cashback tracker for shoppers trying to avoid browser-extension cookie hijacking before Monday, Nov 30 at 11:59 PM PT , I wanted it to persist state across mobile tab reloads in 12 lines of zero-dependency JavaScript . The 12-Line…
Building a persistent three-step checklist widget in 12 lines of vanilla JavaScript is possible without using any frameworks. By leveraging local storage, the widget can retain user progress across multiple tabs and browser sessions.
To create this widget, first insert three checkboxes with a class of "cm-chk" and a data attribute containing a step number (e.g., "s1", "s2", "s3"). Add a div with an id of "cm-prog" to display the current progress.
Next, initialize a JavaScript object called `st` by parsing the value of local storage with a key of "cm90" or defaulting to an empty object. Define a function called `upd` that retrieves all the checkboxes, filters them based on their checked state, and calculates the number of completed steps and the associated monetary value. The progress percentage and monetary value are then updated in the "cm-prog" div.
Attach event listeners to each checkbox, checking the box if its corresponding step is present in the `st` object. When the checkbox's state changes, update the `st` object with the new checked status and store it back in local storage. Finally, call the `upd` function initially to display the initial state.
With this simple yet robust approach, users can seamlessly navigate between cashback portals, retailer checkouts, and even browser extension settings, all while preserving their progress without the need for cookies or additional login walls.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to