A page may show a different status after you click a button. Reading a few lines of JavaScript lets you identify which element changes and why that display change does not itself update a server account. The script-review method still applies: follow the input, action, and effect.
HTML, HyperText Markup Language, describes the structure of a page. A tag uses angle brackets, such as <p>. An element is a structured piece of the document: <p>waiting</p> is a paragraph element with text. An attribute adds information inside an opening tag; id="status" gives that element an identifier named status.
The browser builds an in-memory document structure called the Document Object Model, or DOM. JavaScript is a programming language that can read and change that structure. You only need the calls shown here. Reading the saved source and the predicted text trace is enough for the activity.
Before you begin
- Recommended first
- An editor that works under pressure, Reading and making files, A script that does one real chore, Variables, strings, lists, dicts, conditions, loops
- Estimated time
- 15 min reading, 15 min activity. Setup and optional practice may take longer.
- Where you work
- Read the supplied HTML source and predicted text trace. No server, account, console commands, or database is required. The later Web basics stage explains HTTP; a local file address does not provide an HTTP exchange.
- Without a lab
- Use the complete source and before-and-after trace to predict the displayed text without opening the saved page. Source reading can be completed on a phone; activating a local page can wait. Reading works without JavaScript. Answer checking needs JavaScript and a secure context; saving additionally needs usable browser storage.
After this lesson you can:
- Identify an HTML element, its id, and the DOM text a script changes.
- Trace a click handler without running unknown console code.
- Separate a browser display change from a server permission.
01Example
The worked paragraph starts with waiting. document represents the current document. querySelector("#status") finds the element whose id is status; # is the selector notation for an id. Assigning textContent replaces that element’s text with ready. The fragment was not opened; the result is predicted from the source.
<p id="status">waiting</p>
<script>
document.querySelector("#status").textContent = "ready";
</script>
Predicted DOM text of #status after the script: ready
The paragraph element is the target. No request is described.02The page tree is not the server
A selector is text describing the element to find. querySelector returns the first matching element, or null, meaning no matching element, if it finds none. The dot in document.querySelector accesses a named operation on the document object. The parentheses contain its argument.
An event is something the browser reports, such as a click when a button is activated. addEventListener registers a handler, a function to call when that event reaches its target. In the activity, the function between braces runs after the click rather than while the handler is being registered. Its assignment updates the paragraph’s textContent.
A server is a program that responds to requests; it may store account records and decide permissions. These sample files send no request. Their DOM text is local display state. Writing a role word into that display cannot, by itself, grant server-side rights.
Opening an HTML download later uses a file URL, an address referring to a local file. It does not turn the document into an HTTP exchange. Keep the required activity on source and trace; do not paste unknown code into a browser console or start a server for this lesson.
Selecting #status and assigning textContent changes the local paragraph; no request or server permission change is established.Model. A teaching model drawn by hand. It shows the idea, not real tool output.
Read the full explanation below.
The source and modeled DOM both begin with waiting.
Saved source
Browser DOM
Server / network
This immediate worked assignment changes DOM text only; the later click-handler activity is separate.
The source and modeled DOM both begin with waiting.
Follow the explanation
- The source and modeled DOM both begin with waiting.
- The #status selector identifies the paragraph by id.
- The assignment changes DOM text to ready while the saved source remains intact.
Read every state and value
1. The source and modeled DOM both begin with waiting.
- Example
- before
- Items
- Value: <p id="status">waiting</p> document.querySelector("#status").textContent = "ready";; Status: Saved source · Value: waiting; Status: present · Value: no request described; Status: absent · Value: no change established; Status: unknown
- Active Item
- paragraph
- Event Label
- The source and modeled DOM both begin with waiting.
- Visible Limit
- This immediate worked assignment changes DOM text only; the later click-handler activity is separate.
2. The #status selector identifies the paragraph by id.
- Example
- before
- Items
- Value: <p id="status">waiting</p> document.querySelector("#status").textContent = "ready";; Status: Saved source · Value: waiting; Status: selected · Value: no request described; Status: absent · Value: no change established; Status: unknown
- Active Item
- html · paragraph
- Event Label
- The #status selector identifies the paragraph by id.
- Visible Limit
- This immediate worked assignment changes DOM text only; the later click-handler activity is separate.
3. The assignment changes DOM text to ready while the saved source remains intact.
- Example
- after
- Items
- Value: <p id="status">waiting</p> document.querySelector("#status").textContent = "ready";; Status: Saved source · Value: ready; Status: changed · Value: no request described; Status: absent · Value: no change established; Status: unknown
- Active Item
- paragraph
- Event Label
- The assignment changes DOM text to ready while the saved source remains intact.
- Visible Limit
- This immediate worked assignment changes DOM text only; the later click-handler activity is separate.
03Predict the status line after the button click
The activity download begins with a waiting paragraph and a button named Mark ready. Unlike the worked immediate assignment, its change waits for a click on the button. Compare the handler target with the element whose text it changes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Desk status</title>
</head>
<body>
<p id="status">waiting</p>
<button id="mark" type="button">Mark ready</button>
<script>
document.querySelector("#mark").addEventListener("click", function () {
document.querySelector("#status").textContent = "ready";
});
</script>
</body>
</html>
YanoSec / Code
Predicted DOM trace for dom-demo.html. You can finish this card without opening the page or running the script. A file URL refers to the saved local document; reading it does not create an HTTP exchange.
Before a click:
element p, id status, text waiting
element button, id mark, text Mark ready
Click event on #mark runs the function in the script.
querySelector("#status") selects the paragraph whose id is status.
textContent replaces that paragraph's text with ready.
After the click, in this prediction:
status text: ready
No request leaves the browser.
No server account or permission changes.
- Find the paragraph and button in dom-demo.html. Record paragraph id status with text waiting, and button id mark with text Mark ready.
- Find addEventListener on #mark. The quoted event name is click. The function in braces is the handler that would run when that button is activated.
- Follow the handler to #status and its textContent assignment. Predict ready as the paragraph text after the click. Compare with the before-and-after trace.
- Look for any request or account update in the complete script. None is present. Stop after the source trace; opening the local HTML file later is optional and still does not create a server account.
04Optional challenge
For about ten extra minutes, describe the two roles of the identifiers without changing any source. The button id mark receives the click listener; the paragraph id status receives the text update. Explain why those can be different elements.
05Fresh cases for the checks
Use these fictional cases for the checks below. They have different inputs from the worked examples. Read them here or make notes; no installation or external service is needed.
Page case A: local source modelOwned fixture. An original file made for this course. Follow the activity's instructions in your own lab.
<p id="message">idle</p>
<p id="role">member</p>
<button id="stamp">Mark</button>
<script>
document.querySelector("#stamp").addEventListener("click", () => {
document.querySelector("#message").textContent = "queued";
});
</script>
Events: page loads; stamp is clicked once. This snippet describes no network request.
Page case BOwned fixture. An original file made for this course. Follow the activity's instructions in your own lab.
An independent page changes a displayed role from member to editor using textContent. The server’s stored account role remains member; editing entries requires the server to authorize an editor. No request has yet been sent.
06Explain your prediction
Optional written self-check: Describe the event target, changed element, before/after text and whether any request is shown. Keep a local DOM change separate from server-side roles and authorization.
07Common mistakes
If you predict that the button text changes, compare the selector receiving addEventListener with the selector receiving textContent. They name different targets.
If a selector finds nothing, compare its spelling with the HTML id. The # belongs in the selector string; the id attribute itself contains the name without #.
If the new text is treated as a server permission, look for the request and server-side decision that would make that happen. The supplied script contains neither.
References
- MDN: Basic HTML syntax
Introduces the elements, tags, and attributes used in the supplied page.
- MDN: Document Object Model
Describes the browser’s in-memory representation of a document.
- MDN: Document.querySelector
Documents selector matching and the null result when no element matches.
- MDN: Node.textContent
The value and setter descriptions explain the sample’s text replacement.
- MDN: EventTarget.addEventListener
Describes registering a function to handle a named event.