Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
JAVASCRIPT DOM / CHAPTER 04 / Learn / LESSON 06
For <input>, the value is .value (a string!). Number(value) makes a number for math. Empty string → Number("") is 0 — careful with validation later.
Typical pattern: read value inside a click listener, compute, write the result to another element.
Mini example:
const amount = document.querySelector("#amount"); const out = document.querySelector("#out"); document.querySelector("#run").addEventListener("click", () => { out.textContent = String(Number(amount.value) * 2); });
Common mistake: adding strings ("1"+"2"="12") instead of Number; or reading textContent on an input.
<input id="amount" type="text" value="0" /><button id="run" type="button">Run</button><p id="out">?</p>value is a string — use Number before math.
Press Run — HTML loads in a sandbox iframe; Stop removes the iframe (also kills infinite loops).
›