UNIT 22 • STAGE 7 OF 7
JavaScript multi-step form: classList, addEventListener, required field validation
Right now in Stage 6, all three form steps are visible at the same time. That is fine for styling, but a real multi-step form should show only one step at a time. JavaScript makes this happen by adding and removing a CSS class.
The pattern works in two parts. First, you add a CSS rule that hides every .form-step by default, and a second rule that shows the ones with the class active:
Then JavaScript adds or removes the active class when buttons are clicked. When active is on a step, that step is visible. When it is removed, the step disappears.
element.classList.add('active') adds the class. element.classList.remove('active') removes it. element.classList.contains('active') lets you check if it is already there. You used classList.toggle in earlier units. This is the same API, just with explicit add and remove instead of toggling.
Find your existing .form-step CSS in the <style> block. It probably just sets some margin or padding. Add display: none to it, then add the .form-step.active rule right after:
After you add this, the preview will go blank in the form area. All three steps are now hidden because none of them have the active class yet. That is correct. The JavaScript in the next step will add it to step 1 on page load.
Before the closing </body> tag, add a <script> block with this code:
Here is what you just wrote, broken down:
steps. This is like getElementById but it finds multiple elements at once and returns them all.active from every one, then adds active to just the one at position index. Index 0 is Step 1, index 1 is Step 2, index 2 is the confirmation.alert() shows a message and return stops the function from continuing.trim(), a user who types only spaces would pass the check.showStep. Steps before the current one get the done class (dim teal). The current step gets active (bright teal).Click Next without filling anything in. You should see an alert. Fill in a name and email, click Next, and Step 2 appears. Click Back and you return to Step 1. Click Submit and the confirmation message appears. The dots update as you move through the steps.
You built a full event website across seven stages: a poster-style hero with Oswald display type, event info cards using flexbox, a two-day schedule with a CSS Grid timeline, dance category cards with aspect-ratio, a two-column vendor grid, a styled multi-step form, and JavaScript that controls which step the user sees.
The JavaScript pattern you used here, hiding elements by default and revealing them by adding a class, is one of the most common patterns in web development. Tabs, accordions, modals, carousels, and tooltips all work the same way at their core.
The concepts covered in this unit:
:root variables) as a design system foundationclamp() for fluid responsive typographyflex: 1 and min-width for wrapping cardsgrid-template-columns: 80px 1fr and border-leftaspect-ratio for consistent card proportionsjustify-content: flex-end to anchor content to the bottomflex-shrink: 0; min-widthinput, select, textarea, :focusquerySelectorAll, classList.add/remove, value.trim(), addEventListener