6 The Document Object Model API
This chapter covers
- How the browser maps HTML hierarchy to JavaScript data structures
- Uptating the UI without external libraries
- Working with the DOM API in Vanilla JavaScript
- Querying, modifying, styling, and managing events
In the previous chapter, we covered the current state of JavaScript and ECMAScript syntax. Now it’s time to look at how browser APIs let us manage the user interface in Vanilla JavaScript through the DOM API.
The DOM is the in-memory representation of the document that the rendering engine uses to paint the page. It is a tree of nodes where every element, text node, and comment has a position and relationships to parent, children, and siblings. Understanding that structure is the foundation for manipulating UI with JavaScript.
You do not need a library to work with the DOM. The platform already provides selectors, node constructors, insertion and removal helpers, and a rich event system. Many helpers from the past are no longer necessary today.
Performance matters when changing the DOM. Making many uncoordinated edits can result in additional layout and painting work. Prefer batching changes, using fragments, delegating events, and avoiding forced synchronous layout reads between writes.
This section covers the document model, the most used interfaces, how to query and modify the tree, and the event patterns you will use daily. With these tools, you can build rich interfaces with no external dependencies.