Skip to content
SPM Tuition
Lesson · Computer Science

Tracing a value from HTML input to result

You add two marks on your page and the total joins them instead of adding.

Data on a web page travels in steps: the user types, JavaScript reads the box, the program works with the value, and the result is written back to the screen. The key fact is that a box always gives you text.

This lesson belongs to web development without assessed-project substitution. It uses a practice page, so the code here is not any student’s project.

What path does a value take?

There are four stages. Keep them separate in your head, because a bug lives in exactly one of them.

Stage What happens Where the value is
1. Input The user types in a box Inside the box, as text
2. Read JavaScript reads the box value In a variable, still text
3. Process Convert and calculate In a variable, now a number
4. Output Write the result to an element On the screen, as text

Worked example: adding two quiz marks

A fictional class page has two boxes, quiz1 and quiz2, and a paragraph with id total. Mr Faiz types 14 and 9.

<input id="quiz1"> <input id="quiz2">
<button id="add">Add</button>
<p id="total"></p>
document.getElementById("add").addEventListener("click", function () {
  var a = document.getElementById("quiz1").value;
  var b = document.getElementById("quiz2").value;
  document.getElementById("total").textContent = a + b;
});

Trace it after the click.

Step Code a b Type
1 read quiz1 “14” not yet text
2 read quiz2 “14” “9” text
3 a + b “14” “9” joins to “149”
4 write to total screen shows 149

The page shows 149, not 23. The fault is at stage 3: adding two texts joins them.

The mistake and the fix

A common assumption is that the program knows a box holding 14 contains a number. It does not, so the trace must mark each value as text or number.

The fix converts before adding.

var a = Number(document.getElementById("quiz1").value);
var b = Number(document.getElementById("quiz2").value);
document.getElementById("total").textContent = a + b;

Now a is 14 and b is 9, both numbers, and 14 + 9 = 23. The screen shows 23.

Without Number() With Number()
a “14” 14
b “9” 9
a + b “149” 23

What if the box is empty?

An empty box gives the empty text “”. Number(“”) gives 0, so a blank quiz mark silently counts as zero. Trace this case too, because it shows why validation matters, which the next lesson covers.

Check yourself

Boxes hold “6” and “3”. The code is var r = a * b where a and b come straight from the boxes’ value. What does r hold, and does the page need Number() here?

Answer

JavaScript converts text to numbers when you multiply, so “6” * “3” gives 18. The page would show 18 without Number().

Addition is the exception because + also joins text. Still, converting with Number() in all cases is a safer habit, because it makes each step’s type clear in your trace.

What to study next

Once the value path is clear, test what happens when the input is wrong with separating client-side feedback from server-side validation. Then record your traces using the mistake log and paper-error review.

For a teacher to trace practice pages with you, see online one-to-one Computer Science tuition.

Common questions

Why does my page add two numbers as 149 instead of 23?

Text boxes give you text. Adding two pieces of text joins them, so '14' plus '9' becomes '149'. Convert each value to a number first, for example with Number(), then add.

What is the value property of an input?

It holds whatever the user has typed in that box, always as text. You read it in JavaScript when you need the input, usually after a click or key press, not when the page first loads.

How do I show a result on the page?

Select the element that should display it, then set its textContent to the result. The page updates immediately. You can check the change by looking at the element in the browser.

What is a trace table for a web page?

It lists each step and what each variable or element holds at that step. For a web page the steps include reading the box, converting, calculating and writing the output.

If your totals come out wrong and you cannot see why, a one-to-one Computer Science teacher can trace the value with you on a practice page, so your own project stays yours.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.