JavaScript and the DOM
5 minJavaScript changes a page in three moves: find an element, read its value, write something new. Learn them on a real calculator, and you can read a lot of interface code.
Lesson video
Tastebud · 4:55 · Captions on screen
JavaScript changes a page by finding elements, reading their values and writing new content, inside functions.
- Why. The page is a tree the browser keeps in memory. Scripts change what's on screen by changing that tree, and a function lets the same change run again and again.
- Real example. Change QUANTITY from 400 to 500 and Turnover updates as you type. A function reads the three boxes, turns their text into numbers and writes the new total. (Zerodha, Nov 2026)
- When to break it. Skip JavaScript when HTML already does the job. A details element opens and closes a panel with no script at all, and it works before any JavaScript loads.
- Common mistake. Adding two values read from the page without converting them. "1000" + "1100" joins into "10001100", so Turnover shows 4000440000 instead of 840000.
Our take. Learn three DOM moves before any tour of the language: find an element, read its value, write new text. Most small interface scripts are those three moves, and they're the first thing to check in code an AI tool wrote.
A value from the page is text until you convert it
Every input hands JavaScript its value as text, called a string, even a number box. The + sign joins strings end to end instead of adding them, so wrap each value in Number() before any sum.
- "1000" + "1100"
- gives "10001100"
- Turnover: 4000440000
- Number("1000") + Number("1100")
- gives 2100
- Turnover: 840000
Both sides times a QUANTITY of 400. Only the right one is the real turnover.
A function is a set of steps you can run again
A function puts steps under a name, like showTurnover. The input event calls it on every keystroke, so one function keeps Turnover in step with the three boxes.
Each keystroke runs the same function again
- You to QUANTITY box: type 500.
- QUANTITY box to showTurnover(): input event calls it. addEventListener("input", showTurnover) set this up once.
- showTurnover() to QUANTITY box: reads "500". It reads BUY and SELL the same way.
- showTurnover() to Turnover line: writes 10,50,000. (1,000 + 1,100) times 500, grouped with en-IN.
Write with textContent unless you mean HTML
querySelector finds an element, value reads an input, and textContent writes plain text into an element. innerHTML reads what you give it as HTML, which is risky with anything a person typed.
- turnover.innerHTML = total; reads the value as HTML
- On text a person typed, any markup becomes part of your page
- document.write(total); clears a page that has already loaded
- turnover.textContent = total.toLocaleString("en-IN");
- Writes plain text into the output element
- Groups the digits the Indian way: 8,40,000
- Whatever the value holds shows as text, never as markup
Try it
Fix the Turnover calculator
This is the Intraday equity box from Zerodha's brokerage calculator, recreated Nov 2026. Turnover should be 8,40,000, but it shows a number thousands of times too big. Fix the function in the JS tab: turn the values into numbers, then group the digits the Indian way.
Tab adds two spaces. Press Esc, then Tab, to leave the editor.
- BUY's value is turned into a number (not yet)
- SELL's value is turned into a number (not yet)
- Turnover is written the Indian way, with toLocaleString("en-IN") (not yet)
What to notice: It starts at 4000440000 because "1000" + "1100" joined into "10001100". Once both values are numbers it reads 840000, and with en-IN it reads 8,40,000. Then change QUANTITY and watch Turnover follow as you type.
Quick check
A loan screen adds a processing fee of 2500 to a loan of 500000, both read from input boxes, and shows 5000002500. What do you change?
Watch next
Watch it for What the DOM is, and how JavaScript uses it to change what's on the page.
Watch it for The common ways to select elements and change their text, the same moves the calculator uses.
Connects to: 2.1 How browsers draw a page · 6.3 State and effects · 13.3 Reviewing AI code
Sources · checked Nov 2026
- Zerodha: Brokerage calculatorThe Intraday equity box has BUY, SELL and QUANTITY inputs (1000, 1100 and 400 to start), a Turnover line, and runs its function on every input event
- Zerodha: brokerage.js (the calculator's script)cal_intra() reads each box's value, runs parseFloat on it, works out turnover as (buy + sell) x quantity and writes it into the page
- MDN: HTMLInputElement valueAn input's value property is a string
- MDN: Addition (+)If one side of + is a string, both are joined as strings ("2" + 2 gives "22")
- MDN: Number.prototype.toLocaleString()"en-IN" groups digits in lakhs and crores (123456.789 gives 1,23,456.789); Baseline widely available since July 2015
- MDN: Introduction to the DOMThe DOM represents the page as a tree in memory that scripts can change; querySelector finds elements and textContent sets their text
- MDN: Element innerHTML, security considerationsinnerHTML parses its value as HTML and is a common route for cross-site scripting; use textContent for plain text
- MDN: Document write()Calling document.write() on a loaded page clears it, and its use is strongly discouraged
- MDN: The details elementdetails is a disclosure widget that opens and closes with no script; Baseline widely available since January 2020 (the breakIt line)