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;
.valueis a string, always."25", not25.Number(...)converts it.Number("")is0, so check for empty fields too.- For a radio group, select the checked one with
input[name="sex"]:checked. - For a select,
.valueis the selected option’svalue.
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.
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 stepAdd 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
clickon the button instead ofsubmiton the form. Enter stops working. - Not converting strings to numbers.
"70" + 170is not240. - 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,
.valueonnullthrows. - Writing the result with
innerHTML. UsetextContentunless 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.
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.