Selector API

> (World Wide) Web - (W3|WWW) > Selector API

1 - About

A selector is a pattern that match against elements in a DOM tree in order to select one or several node.

This API was known before as the CSS Selector API and was renamed to show that the selection is not only used by CSS but also by other technoclogy such as Javascript DOM selection.

A basic selector is a simple boolean predicate that:

  • takes an element in a tree structure
  • and tests whether the element matches the selector or not.

A selector can be composed of several predicate expression via logical operator

This mini-language consists of predicates that filter elements by:

Advertising

3 - Usage

4 - Syntax

See Selector Overview.

Type Example Description
Universal Selector * Any element
Tag selector E An element of type E
Id Selector E#myid An E element with ID equal to myid
Id Selector #myid Any element with ID equal to myid (equivalent to *#myid)
Attribute selector E[foo] An E element with a foo attribute
Attribute selector E[foo=“bar”] An E element whose foo attribute value is exactly equal to bar

Type:

Advertising

5 - Sibling

5.1 - Adjacent

Format:

element1 + element2

The selector matches if E1 and E2 share the same parent in the document tree and E1 immediately precedes E2, ignoring non-element nodes (such as text nodes and comments).

On this website, this selector will select the link “Nico” and as it is the last child, there will be no others children in the selection.

#header .description > a+a+a+a

6 - Reference

web/selector/selector.txt · Last modified: 2019/02/09 11:44 by gerardnico