Element, EventTarget, or CSS selector.
Properties (e.g. someProperty
) or attribute names (e.g. data-some-property
) for the dataset entry.
Object with key of keys
and corresponding dataset property values (or undefined
if not present).
The arcade!WithUndefinedValues
type represents an object with values that could be 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")!;
type AttributesShape = {
"data-label": string | undefined;
"data-count": number | undefined;
};
getDatasetEntries<AttributesShape>(element, [
"data-label",
"data-count",
]);
// { "data-label": "Example", "data-count": 30 }
Using Property Names (camelCase)
const element = findElement("#example")!;
type PropertiesShape = {
label: string | undefined;
count: number | undefined;
};
getDatasetEntries<PropertiesShape>(element, ["label", "count"]);
// { label: "Example", count: 30 }
Builds an object with the values associated with the dataset
keys
on thetarget
. If any of the specifiedkeys
don't exist, they are set toundefined
in the return value.You will need to perform checks for whether a value is
undefined
in the returned object if some of the entries weren't present. See the code block below for additional details.