• 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.

    Type Parameters

    • V extends DatasetValue = string

      Type of value to return for the corresponding key.

    Parameters

    • target: null | Target

      Element, EventTarget, or CSS selector.

    • key: string

      Property (e.g. someProperty) or attribute name (e.g. data-some-property) for the dataset entry.

    Returns V | undefined

    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