DOM Manipulation in JavaScript
DOM Manipulation = how JavaScript reads and changes a web page.
This chapter has 5 lessons and 24 exercises you run and check in your browser. Plan on about 123 minutes.
- DOM Fundamentals: finding, creating, adding and removing elements on a page.
- DOM Content Management:
textContent,innerHTMLand attributes. - DOM Styling: inline styles and classes with
classList. - DOM Events and Forms: clicks, typing and form submissions, and checking what users enter.
- DOM Traversal, Collections and Animation: moving between elements, element lists and simple animation.
After this chapter, you can:
- Find any element on a page and change it.
- Show text safely and manage attributes and classes.
- Respond to clicks, typing and form submissions.
- Move between related elements and add simple animation.
Every lesson has short explanations, code boxes you edit and run in the live editor, and a web page example you can click.
Before you start, finish Object-Oriented JavaScript.
Next chapter: Modules and Clean Coding.
Lessons in this section
- DOM Fundamentals
What the DOM is, DOM methods and properties, finding elements by id, class, tag and selector, and creating, adding, replacing, removing and deleting elements through the document object.
- Explain what the DOM is and tell a DOM method from a DOM property
- Find elements with getElementById, getElementsByClassName, getElementsByTagName, querySelector and querySelectorAll
- Handle the null that a failed search returns
- Tell a live HTMLCollection from a static NodeList
- DOM Content Management
Reading and changing content with innerHTML, textContent and outerHTML, adding, replacing and removing HTML, and working with attributes, properties and form attributes through getAttribute, setAttribute, removeAttribute and hasAttribute.
- Choose between innerHTML, textContent and outerHTML
- Add HTML with insertAdjacentHTML without rebuilding existing elements
- Replace and remove the content of an element
- Convert text read from the page before doing arithmetic
- DOM Styling
Changing inline styles, class-based styling, reading final styles with getComputedStyle, setting custom properties, and adding, removing, toggling and checking classes through classList and the DOMTokenList it returns.
- Set, read and clear inline styles with the style property
- Read the final style of an element with getComputedStyle
- Explain why class-based styling is usually better than inline styles
- Set a CSS custom property from JavaScript with style.setProperty
- DOM Events and Forms
Attaching and removing event listeners, the everyday events from click to blur, and reading, updating and validating the controls in a form.
- Attach a handler with addEventListener() and remove it with removeEventListener()
- Choose the right event among click, dblclick, mouseover, mouseout, keydown, keyup, input, change, submit, focus and blur
- Read event.key and event.target from the event object
- Access form controls through form.elements and read or update their values
- DOM Traversal, Collections and Animation
Stepping between parent, child and sibling elements, working with HTMLCollection and NodeList, live and static collections, and moving, showing, hiding and fading elements.
- Step to an element's parent, children and siblings
- Explain the difference between nodes and elements
- Tell an HTMLCollection from a NodeList, and a live collection from a static one
- Loop over a collection and convert a NodeList into an array