JomMula

Frontend Development: HTML to React · JavaScript · Lesson 17

Events and forms: make it react

A form that doesn't submit anywhere, a button that listens, and a result that updates. This is the lesson where the calculator finally calculates.

8 min readJavaScripteventsformsUpdated October 2026

The page is built. The CSS looks good. Now we need to connect the form to the math.

That connection is an event: the browser tells you something happened, and you run a function in response.

addEventListener

const button = document.querySelector("#calculate");

button.addEventListener("click", () => {
  console.log("Clicked!");
});

addEventListener(eventName, handler) registers a function to run when the event happens on that element.

Common events:

  • click — a button, link, or anything clickable.
  • submit — a form is submitted (button click or Enter).
  • input — the value changes as the user types.
  • change — the value changes and the field loses focus (good for selects).
  • keydown — a key is pressed.

The event object and preventDefault

form.addEventListener("submit", (event) => {
  event.preventDefault();
  // handle the form ourselves
});

By default, submitting a form sends a request and reloads the page. We don’t want that — we want to calculate in the browser. event.preventDefault() stops the default behavior.

Reading form values

const age = Number(document.querySelector("#age").value);
const activity = document.querySelector("#activity").value;
const sex = document.querySelector('input[name="sex"]:checked').value;
  • .value is a string, always. "25", not 25.
  • Number(...) converts it. Number("") is 0, so check for empty fields too.
  • For a radio group, select the checked one with input[name="sex"]:checked.
  • For a select, .value is the selected option’s value.

Updating the result

const result = document.querySelector("#result");
result.textContent = "Your target is " + Math.round(target) + " kcal";

That’s it. Select the element once, update its text when the calculation is done.

Try it

This is a working BMR calculator. Change the values, press Calculate, and watch the result update.

Preview

Console

Console is quiet. Run the code to see output.

Edit the code, then hit Run (or Ctrl + Enter) and watch the preview. This is a safe sandbox — you can't break the page.

Wire your own form

the plan's verify step

Add a submit listener to your calculator form. Prevent the default, read every field, calculate BMR and TDEE, and write the result into the #result paragraph. Then add a goal select: maintain, lose, or gain, and adjust the target by -500, 0, or +300.

Test the empty form and a very large number. The browser's built-in validation should catch most of it; your code should handle the rest.

The mistakes everyone makes

  • Forgetting event.preventDefault(). The page reloads and your result disappears.
  • Listening for click on the button instead of submit on the form. Enter stops working.
  • Not converting strings to numbers. "70" + 170 is not 240.
  • Reading values outside the submit handler. They change as the user types.
  • Forgetting .value. You get the input element, not its value.
  • Assuming a radio is checked. If none is, .value on null throws.
  • Writing the result with innerHTML. Use textContent unless you deliberately want markup.

addEventListener connects an event to a function.

Listen for submit on the form, not click on the button.

preventDefault stops the browser's default behavior.

Form values are strings; convert them with Number().

Update the page with textContent after the calculation.

Next: state and localStorage — keeping the food log alive when the page refreshes.

Previous: The DOM: select, read, change
18

Up next

State and localStorage: remember it

The food log needs to remember what you added — even after a refresh. Learn state, a render function, and localStorage: the same idea React will build on.

Read now