Working with the DOM

In the last chapter, our focus was strictly on the JavaScript language and syntax. In this chapter, we will learn how to use JavaScript to manipulate the DOM. But first, what is the DOM?

Updated

Introducing the DOM

  1. Introducing the DOM: an overview (Novice): When the browser loads a page, it turns the HTML into the DOM, a tree of objects with one for each element, and JavaScript reads and changes the page through that tree.
  2. Locating DOM elements (Apprentice): Most searches start at the document, the whole page, where getElementById finds an element by its id, querySelector finds the first match for a CSS selector, and querySelectorAll finds every match.

Working with Elements

  1. Working with Elements: an overview (Apprentice): Once you've found an element, JavaScript can read it and change it, from its text and CSS classes to which elements are on the page at all.
  2. Common DOM Interactions (Apprentice): Once you have an element, you change its text with textContent, its HTML with innerHTML, its look with style, and its CSS classes with classList.
  3. Manipulating the DOM Structure (Apprentice): createElement makes a new element, appendChild puts it inside a parent so it shows on the page, and remove takes an element off the page.