Skip to Content
PureHTMLdocs
Transformers

attr

Extract element attributes as strings or objects.

The attr transformer extracts individual HTML attributes, all attributes, or a subset of attributes.

Extract a Single Attribute

Pass the attribute name as an argument (e.g. attr(data-id) or attr(href)):

{ selector: div, transform: 'attr(data-id)' }

Extract All Attributes

Invoke attr with no arguments (attr() or attr) to extract an object containing all element attributes:

{ selector: div, transform: 'attr()' }

Extract a Subset of Attributes

Pass multiple comma-separated attribute names:

{ selector: div, transform: 'attr(data-first-name, data-last-name)' }

On this page