The dataset property allows you to access custom data attributes defined in HTML elements. These attributes are particularly useful for attaching metadata to elements that can later be accessed using camel case or bracket notation. These attributes are prefixed with data-
.
The attribute names in HTML use kebab-case, data-user-id
and are converted to camelCase in JavaScript, dataset.userId
.
In this example, the custom data-order-id
and data-bill-id
attributes store the order and bill details. We then fetch and display them dynamically.
In this example, the custom data-tracking-number
and data-customer-id
attributes store shipment tracking and customer details. These attributes are accessed dynamically using bracket notation. This is particularly useful when dealing with complex attribute names, such as those with hyphens.