Type of value to return for the corresponding key.
Element, EventTarget, or CSS selector.
Property (e.g. someProperty
) or attribute name (e.g. data-some-property
) for the dataset entry.
Value of the dataset property associated with key
, otherwise undefined
.
elements!InvalidElementError
if the specified target
wasn't found.
HTML
<div
id="example"
data-is-active="false"
data-count="30"
data-label="Example"
>
...
</div>
Using Attribute Names (data-*
)
const element = findElement("#example")!;
getDatasetValue(element, "data-label");
// "Example"
getDatasetValue(element, "data-count");
// 30
getDatasetValue(element, "data-is-active");
// false
Using Property Names (camelCase)
const element = findElement("#example")!;
getDatasetValue(element, "label");
// "Example"
getDatasetValue(element, "count");
// 30
getDatasetValue(element, "isActive");
// false
Attempts to get the value associated with the dataset attribute/property name
key
on thetarget
. Returnsundefined
if no entry was found for the specifiedkey
.