This module provides functions for querying and manipulating the dataset entries on HTML and SVG elements.

Classes

DatasetWrapper

Wrapper for managing the dataset property on an element.

Type Aliases

AnyDatasetShape

Valid shape for dataset property. The values can be any type that can be stringified. Used for defining the shape in the DatasetWrapper class.

Dataset

Valid key/value pair representing dataset attributes/properties (prior to stringifying). The key should be a valid DatasetKey and the value must be a valid DatasetValue. Some of the values may be null or undefined.

DatasetAttributeName

Valid name for dataset data-* attributes on a HTML/SVG element (e.g. data-some-value).

DatasetKey

Valid name for Dataset entries. Represents either a key for the HTMLElement.dataset property or a name for the data-* attribute.

DatasetPropertyName

Valid type for the key of HTMLElement.dataset property entries in HTML/SVG elements.

DatasetSearch

Search criteria for checking if dataset entries are present in an element. You can use an array of dataset property/attribute names to check only if the dataset entries are present, or an object to search for specific values. Use null for the value if you only care about the presence of a dataset entry.

DatasetValue

Value that can be assigned to or extracted from a dataset property (prior to stringifying).

Functions

getDatasetEntries

Builds an object with the values associated with the dataset keys on the target. If any of the specified keys don't exist, they are set to undefined in the return value.

getDatasetValue

Attempts to get the value associated with the dataset attribute/property name key on the target. Returns undefined if no entry was found for the specified key.

hasAllDatasetEntries

Checks if the target has all of the dataset entries that match the search criteria.

hasDatasetEntry

Returns true if the target has a dataset entry with key and optionally, the matching value.

hasSomeDatasetEntries

Checks if the target has some of the dataset entries that match the search criteria.

removeDatasetEntries

Removes the dataset entries with the attribute/property names keys from the target.

removeDatasetEntry

Removes the dataset entry with attribute/property name key from the target.

selectDatasetEntries

Attempts to build a CSS selector string for the specified dataset object. Note that the values of the dataset object are coerced to a string and null excludes a value but only includes a key. If tagName is specified, it is included in the resulting selector.

selectDatasetEntry

Attempts to build a valid selector for a dataset with the specified key and optional value. If tagName is specified, it is included in the resulting selector.

setDatasetEntries

Assigns the dataset key/value pairs to the target.

setDatasetEntry

Assigns the value to the dataset attribute/property name key in the target.

wrapDataset

Creates a new DatasetWrapper instance for managing the dataset property on the target element. Optionally pass in initialData that can fully or partially match the shape specified in the DS generic.