liveBook · Manning

Chapter 12: Chapter 12. Working the DOM

What you will learn in chapter 12 of Secrets of the JavaScript Ninja, Second Edition

The introduction explains why DOM modification matters for dynamic web applications and why understanding it can improve performance even when using a library.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why DOM modification matters for dynamic web applications and why understanding it can improve performance even when using a library.

Key terms: Document Object Model (DOM), modifying the DOM, DOM modification, performance, JavaScript library

12.1 Injecting HTML into the DOM

This section motivates injecting HTML strings into the DOM for dynamic pages and explains the tradeoff with constructing elements through a DOM API.

Key terms: HTML string, DOM, client-side templates, DOM construction API

12.1 Do you know?

This section poses three questions to check the reader’s understanding of HTML injection, DOM fragments, and measuring hidden elements.

12.1.1 Converting HTML to DOM

This section explains how to turn an HTML string into DOM nodes by correcting problematic markup, adding required wrappers, and parsing it with innerHTML.

Key terms: self-closing elements, preparse, HTML string, HTML wrapping, container elements, innerHTML, dummy DOM element, wrapper map, DOM nodes

12.1.2 Inserting elements into the document

This subsection shows how to collect converted DOM nodes in a fragment so they can be inserted and cloned efficiently, including at multiple document locations.

Key terms: DOM fragments, DOM nodes, clone, getNodes, fragment parameter, HTML string, DOM elements

12.2 Using DOM attributes and properties

This section explains how DOM attributes relate to element properties and how to access attributes that lack corresponding properties.

Key terms: getAttribute, setAttribute, element properties, id attribute, id property, custom attributes, data-

12.3 Styling attribute headaches

Introduces the distinction between a style attribute and an element’s style property, and defines computed style.

Key terms: style attribute, style element property, style values, computed style, inherited, applied style information

12.3.1 Where are my styles?

This section explains why an element’s style property contains inline and script-assigned styles but not styles applied from style sheets.

Key terms: style property, style attribute, style object, style sheets, borderWidth, style sheet, !important

12.3.2 Style property naming

This section explains how CSS style names are accessed in JavaScript, demonstrates a helper for getting and setting styles, and notes a limit of an element’s style property.

Key terms: CSS attributes, dot operator, camel case, style function, regular expression, name parameter, getter, setter, style property, style sheets

12.3.3 Fetching computed styles

This section explains what contributes to an element’s computed style and how to retrieve individual computed CSS property values.

Key terms: computed style, style sheets, style attribute, getComputedStyle, getPropertyValue, camel-cased, dashed, amalgam properties, border, border-top-color

12.3.4 Converting pixel values

Explains how to set pixel-based style values reliably, avoid adding pixel units to unitless properties, and read numeric pixel values.

Key terms: style values, pixels, px, nonpixel values, parseFloat, style attribute, pixel value

12.3.5 Measuring heights and widths

This section explains how to measure element dimensions, including elements hidden with display: none, without visibly disturbing the page.

Key terms: height, width, auto, offsetHeight, offsetWidth, padding, display, block, visibility, hidden

12.4 Minimizing layout thrashing

This section explains why alternating DOM writes with layout reads hurts performance and how batching updates reduces unnecessary layout recalculations.

Key terms: layout thrashing, layout, clientWidth, batch, DOM reads, DOM writes, React, virtual DOM, actual DOM

12.5 Summary

12.6 Exercises

Where this is used later

Concepts explained in this chapter

← Chapter 11: Chapter 11. Code modularization techniques Chapter 13: Chapter 13. Surviving events →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.