liveBook · Manning

CSS selector

As explained in D3.js in Action, Third Edition

A CSS selector is a syntax pattern that identifies elements in a document according to their names, attributes, classes, IDs, or relationships.

A CSS selector is a pattern that identifies DOM elements by tag name, class, ID, or a combination of these. D3 passes selector strings to d3.select() and d3.selectAll() before modifying the selected elements. section 2.3 section 2.7

Use selectors to target elements for visualization work:

  • Use d3.select() for the first matching element and d3.selectAll() for every matching element; commas can group selector alternatives. section 2.3

Extracting tables from a web page

A browser script uses document.querySelectorAll(".wikitable.sortable") to collect matching HTML tables, then loops through their rows to extract painting data. The selector was chosen by inspecting the page markup for a unique common pattern. section 14.1

How D3.js in Action, Third Edition covers it

How deep it goes
Explains tag, class, ID, descendant, and grouped selectors with code, then distinguishes first-match and all-match selection.
Examples
Uses headings, paragraphs, circles, a visualization container, and Wikipedia tables as selector targets.
What you can do after
Apply it via D3 and browser DOM methods to select one element, many elements, descendants, or grouped targets.
How it connects
Connects CSS selectors with the DOM, D3 selections, d3.select(), d3.selectAll(), and querySelectorAll().

Also explained in

Compare how 8 books explain CSS selector →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.