Sub topic 2
Inspecting and editing the DOM
This chapter covers the Elements panel: how it differs from "view source", how to select and change nodes, and how to catch scripts that change the page. It assumes you can read basic HTML, know what an attribute and a tag are, and have run a few lines of JavaScript. Panel names follow Chromium-based browsers (Chrome, Edge). Firefox calls its equivalent the Inspector, and Safari calls it the Elements tab.
The Elements panel shows the live DOM
The browser downloads HTML text and parses it into the Document Object Model (DOM). The DOM is a tree of objects that scripts and the rendering engine work with. The Elements panel displays that tree as it exists right now. It does not show the original file.
This matters because the two often differ:
- Scripts add, remove and rewrite nodes after the page loads.
- The parser repairs invalid markup and inserts implied elements.
- Frameworks such as React or Vue build most of the page in JavaScript, so the downloaded HTML may be an almost empty shell.
To see the difference, compare "View page source" with the Elements panel on any page that loads content with JavaScript. View source shows what the server sent. The Elements panel shows what the browser holds in memory.
Parser corrections
Suppose your source contains this markup:
<table>
<tr><td>Total</td><td>42</td></tr>
</table>
<p>Intro <div>Block inside a paragraph</div></p>The Elements panel shows something different:
- The table gains a
<tbody>element that you never wrote. The HTML parsing rules insert it. - The
<p>closes before the<div>, because a paragraph cannot contain a block element. The trailing</p>becomes an empty paragraph.
A CSS selector such as table > tr that matches your source will fail against this DOM. When a selector or script behaves unexpectedly, check the tree the browser actually built.
Opening the panel and selecting elements
You can reach the Elements panel in several ways:
- Right-click any element on the page and choose Inspect. This opens DevTools with that node selected.
- Press
Ctrl+Shift+C(Cmd+Shift+Con macOS) to turn on the element picker. Hover over the page to see boxes highlight, then click to select. - Press
F12, orCtrl+Shift+I(Cmd+Option+I), and select a node in the tree.
Once a node is selected, the panel gives you several ways to work with it:
- Highlighting: Hovering a node in the tree highlights it on the page, with its dimensions in a tooltip. If nothing highlights, the element may be hidden, have zero size, or be off-screen.
- Keyboard navigation: Arrow keys move through the tree. Right and left arrows expand and collapse nodes.
Alt+click(Option+click) on an expand arrow opens the whole subtree. - Search:
Ctrl+F(Cmd+F) inside the Elements panel searches the tree. It accepts plain text, CSS selectors and XPath expressions, soul.menu > li:nth-child(2)works. The search bar shows the match count and lets you step through matches. - Scroll into view: Right-click a node and choose Scroll into view to find it on the page.
Copying a reference to a node
The right-click menu under Copy offers several forms of the selected node:
| Option | What you get | Typical use |
|---|---|---|
| Copy outerHTML | The element and its children as markup | Reproducing a bug in a test case |
| Copy selector | A CSS selector that identifies the node | Writing a test or query |
| Copy JS path | A `document.querySelector(...)` expression | Pasting into the Console |
| Copy XPath | An XPath expression | XPath-based test tools |
| Copy styles | The computed CSS rules for the node | Sharing style details |
Generated selectors are often long and tied to the current structure, such as #app > div:nth-child(3) > ul > li:nth-child(2). They break when the layout changes. Use them as a starting point, and replace them with a selector built on a stable class or attribute before putting them in a test.
Editing the DOM
Most nodes in the tree can be edited in place. None of the changes touch the file on the server.
- Edit text or a tag name: Double-click it in the tree, type, and press
Enter. - Edit an attribute: Double-click the attribute name or value. To add one, double-click the tag, or right-click and choose Add attribute. Pressing
Tabmoves to the next field. - Edit a whole subtree: Right-click and choose Edit as HTML (or press
F2). You get a text box holding the element'souterHTML. Clicking outside it commits the change. - Move a node: Drag it to a new position in the tree.
- Delete a node: Select it and press
Delete. - Hide a node: Press
Hwith the node selected. This togglesvisibility: hiddenand keeps the element's layout space. It is less destructive than deleting. - Duplicate a node: Right-click and choose Duplicate element.
- Undo:
Ctrl+Z(Cmd+Z) reverses DOM edits while the Elements panel has focus.
Typical uses of live editing include testing a copy change, checking how a long label wraps, and removing an overlay that blocks clicks. The last case is common when an invisible element covers a button, and deleting the suspect node confirms it is the cause.
Why your edit may seem to vanish
Scripts can overwrite your changes while the page is still open. A framework that re-renders a component replaces the nodes it owns, along with your edits. If a manual change disappears after you click something, a script rewrote that part of the tree. That is useful information, because it tells you the framework controls that node and your fix belongs in the component code.
Using the selected node from the Console
The Elements and Console panels share state. The selected node is available in the Console as $0, and earlier selections as $1 through $4. The Console chapter covers utilities in detail. Here is the pattern that matters for DOM work:
// Select an element in the Elements panel first, then run in the Console.
$0.tagName; // "LI"
$0.parentElement; // the parent node
$0.children.length; // number of child elements
$0.dataset; // data-* attributes as an object
$0.setAttribute('aria-label', 'Close');
$0.remove(); // same as pressing Delete in the treeTo keep a reference after you select something else, right-click the node and choose Store as global variable. DevTools creates a variable such as temp1 that you can use in the Console. This helps when you want to compare two nodes, because $0 changes every time you click.
// After storing two nodes as temp1 and temp2:
temp1.isEqualNode(temp2); // true if structurally identical
temp1.compareDocumentPosition(temp2); // bitmask describing their order
getComputedStyle(temp1).display; // style as the browser resolved itFinding out what changes the DOM
When a node changes and you do not know why, DOM breakpoints stop the script at the moment of change. Right-click a node, choose Break on, and pick one of three options:
- Subtree modifications: Pauses when a child is added, removed or moved anywhere inside this node.
- Attribute modifications: Pauses when an attribute on this node is added, removed or changed. This includes
classandstylechanges. - Node removal: Pauses when this node is removed from the document.
When the breakpoint fires, DevTools switches to the Sources panel, highlights the line of script that made the change, and shows the call stack. Reading that stack up from the top shows which function modified the node and which event or timer caused it. The next chapters on the Console and on breakpoints build on this.
A list of breakpoints appears in the Elements sidebar under DOM Breakpoints, where you can toggle or remove them. They are tied to the node object. If a script replaces the node with a new copy, your breakpoint stays attached to the old, detached one and never fires again. Set it on the parent with Subtree modifications instead.
Edge cases to know
Pseudo-elements are not DOM nodes
::before and ::after appear in the tree as labeled entries, but they are generated by CSS. You cannot edit their text in the Elements panel. Change the content property in the Styles pane instead.
Whitespace and comments
Text nodes made only of whitespace affect inline layout, for example the gap between two inline-block elements. The tree usually hides them. If a mysterious gap appears between inline elements, the whitespace in your source is the likely cause.
Shadow DOM
Web components can hide their internals in a shadow tree. DevTools shows #shadow-root (open) as an expandable node under the host. A closed shadow root cannot be reached from page JavaScript, but DevTools can still inspect it. Built-in controls such as <video> and <input type="date"> have internal shadow trees that are hidden by default. Enable Show user agent shadow DOM in DevTools settings under Preferences, then Elements, if you need to see them.
Iframes
Each iframe has its own document. Its content appears nested inside the <iframe> node in the tree, and for cross-origin frames DevTools may show it as a separate target. The Console runs in the top frame by default. Use the context selector at the top of the Console to switch to the frame you are inspecting. Otherwise $0 and document refer to the wrong document.
Attributes versus properties
The tree shows attributes from the markup. JavaScript often works with properties, and the two can disagree. After a user types in an input, the value attribute in the tree still shows the original text, while $0.value returns the current one. If the Elements panel and your script seem to disagree, check whether you are looking at an attribute or a property.
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| Selector copied from source finds nothing | The parser added or moved elements, such as `tbody` | Build the selector from the DOM shown in Elements |
| Element is in the tree but not visible | `display: none`, zero size, or covered by another element | Select it, then check the box dimensions and the node above it |
| Edit disappears on its own | A framework re-rendered the component | Find the code that owns the node |
| `$0` is `undefined` or wrong | Nothing selected, or the Console is in another frame | Select a node and check the Console context selector |
| DOM breakpoint never fires | The node was replaced with a new object | Break on the parent's subtree instead |
| Edit as HTML on `<html>` or `<body>` has odd results | The parser reparses the whole fragment | Edit a smaller subtree |
A repeatable workflow
- Select the element with the picker or right-click, and read its place in the tree.
- Check the parent chain for unexpected wrappers or repairs made by the parser.
- Edit text, attributes or structure to test your hypothesis.
- If something else changes the node, set a DOM breakpoint and read the call stack.
- Move the working change into your source code and reload to confirm it.
Exercise
A page's source has <ul><li>One</li></ul>, and a script runs document.querySelector('ul').innerHTML = '' after a button click. You select the ul in the Elements panel, then add a DOM breakpoint. Which breakpoint type pauses at the script line that empties the list, and why does an "Attribute modifications" breakpoint not work for this?
Show answer
Use "Subtree modifications" on the ul. Setting innerHTML to an empty string removes the li child, which is a change to the node's subtree. "Attribute modifications" fires only when an attribute on the ul itself is added, removed or changed, and no attribute changes here. When it pauses, the Sources panel shows the innerHTML line and the call stack that led to it.
Exercise
You change a button's text with a double-click in the Elements panel and see the new text on the page. After you click the button, the old text returns. Give one likely cause and one way to confirm it.
Show answer
A script, probably in a framework component, re-rendered the button and replaced the node or its text. To confirm, set a "Subtree modifications" or "Node removal" breakpoint on the button or its parent, click the button again, and read the call stack when it pauses.