Sub topic 2
Inspecting and Editing the DOM
This chapter assumes you know basic HTML and CSS, basic JavaScript syntax, and how a browser loads a page. Panel names follow Chromium-based browsers. Firefox and Safari have equivalent tools under similar names.
What the Elements panel shows
The Elements panel displays the live DOM (Document Object Model), the tree of objects the browser builds from your HTML. It is not the same as "View Source". View Source shows the text the server sent. The Elements panel shows the tree as it is now, after scripts have added, removed or changed nodes.
This difference explains many confusing bugs. If a script inserts a button, the button is in the Elements panel but not in View Source. If the HTML has invalid nesting, the browser repairs it while parsing, and the panel shows the repaired tree.
Selecting elements
There are three common ways to select a node.
- Press the inspect tool (the arrow icon at the top left of DevTools, or Ctrl+Shift+C / Cmd+Shift+C), then click the element on the page.
- Right-click an element on the page and choose Inspect.
- Click a node in the tree.
The selected node is highlighted on the page. Hovering over a node in the tree highlights it on the page too. This helps when an element is invisible or has zero size.
Editing the DOM live
You can change the DOM directly in the panel. These edits live only in the current page session. Reloading the page discards them, and your source files never change.
| Action | How |
|---|---|
| Edit text or an attribute | Double-click the text or attribute value |
| Edit a whole element | Right-click the node, then **Edit as HTML** |
| Add an attribute | Right-click the node, then **Add attribute** |
| Delete a node | Select it and press Delete |
| Move a node | Drag it to a new position in the tree |
| Toggle a state such as `:hover` | Right-click the node, then **Force state** |
| Hide a node | Select it and press H |
Use these edits to test a fix before you touch your code. For example, change a class name to see if a style applies, or delete an overlay to check whether it blocks clicks.
Working with the selected node
Each time you select a node, DevTools stores a reference to it as $0 in the Console. The previous selection is $1, and so on. This links the Elements panel to code you run:
// Run in the Console after selecting an element in the Elements panel.
$0.classList.toggle('hidden'); // $0 is the currently selected element
$$('img').length; // $$ is shorthand for document.querySelectorAll, returned as an array
copy($0.outerHTML); // copy() puts a value on the clipboardThe Console chapter covers these utilities in more detail. Here, the point is that you can select a node by clicking, then manipulate it with code.
Finding what changed the DOM
When an element changes and you don't know why, use DOM breakpoints. Right-click a node and choose Break on. You get three options:
- Subtree modifications: pauses when a child is added, removed or moved.
- Attribute modifications: pauses when an attribute on this node changes.
- Node removal: pauses when the node is removed from the tree.
When one fires, execution stops in the Sources panel at the line of script that made the change. The call stack then shows how the code got there. This is often faster than searching your code for every place that touches a class name.
The panel also flashes a node briefly when it changes, which helps you spot updates that happen on their own.
Common errors
- Expecting edits to persist. Live edits vanish on reload. Copy the working change into your source files before you refresh.
- Debugging against stale source. If the tree looks different from your HTML, a script or the parser changed it. Check the tree, not the file.
- Selecting the wrong node. Overlapping elements can hide the one you want. Use the tree, or press the inspect tool and click again, to reach covered nodes.
- Searching only visible text. Press Ctrl+F / Cmd+F inside the Elements panel. It searches by text, selector and XPath across the whole tree, including nodes that are not visible.
- Forgetting the shadow DOM. Elements inside a web component's shadow root appear under a
#shadow-rootnode. You must expand it to see them.
Workflow summary
- Select the node that looks wrong.
- Compare the tree with what you expected.
- Edit the DOM or attributes to test a fix.
- If a script is changing the node, set a DOM breakpoint.
- Move the confirmed fix into your source files.
Exercise
A button appears on your page but you cannot find it in View Source. You also want to know which script adds a disabled attribute to it a moment after load. Which panel features do you use to explain both facts?
Show answer
The button is missing from View Source because a script inserted it after the server sent the HTML. The Elements panel shows the live DOM, so you can find it there. To find which script adds disabled, right-click the button in the Elements panel, choose Break on, then Attribute modifications, and reload. The debugger pauses at the script line that sets the attribute, and the call stack shows how it was reached.
Exercise
You select a div in the Elements panel. What does $0 return in the Console, and how can you copy that element's HTML to the clipboard?
Show answer
$0 returns the currently selected div. Run copy($0.outerHTML) in the Console to put its HTML on the clipboard.