files/en-us/web/api/htmlelement/dataset/index.md
{{APIRef("HTML DOM")}}
The dataset read-only property
of the {{DOMxRef("HTMLElement")}} interface provides read/write access to custom data attributes
(data-*) on elements. It exposes a map of strings
({{domxref("DOMStringMap")}}) with an entry for each data-* attribute.
[!NOTE] The
datasetproperty itself can be read, but not directly written. Instead, all writes must be to the individual properties within thedataset, which in turn represent the data attributes.
A {{domxref("DOMStringMap")}}.
An HTML data-* attribute and its corresponding DOM
dataset.property modify their shared name according to where
they are read or written:
data-. It can contain only letters,
numbers, dashes (-), periods (.), colons (:),
and underscores (_). Any {{Glossary("ASCII")}} capital letters (A to
Z) are converted to lowercase.data- prefix. Single dashes (-) are removed, and the next ASCII
character after a removed dash is capitalized to form the property's camel-cased name.Details and examples of converting between the HTML and JavaScript forms is described in more detail in the next section.
In addition to the information below, you'll find a how-to guide for using HTML data attributes in our article Using data attributes.
dash-style to camelCase conversion
A to
Z);data- (including the dash);U+002D) followed by an ASCII lowercase letter
a to z, remove the dash and uppercase the letter;camelCase to dash-style conversion
a to
z;data- prefix;A to Z,
then lowercase the letter;For example, a data-abc-def attribute corresponds to
dataset.abcDef.
element.dataset.keyname.element.dataset['keyname'].in operator can check if a given attribute exists:
'keyname' in element.dataset. Note that this will walk the prototype chain of dataset and may be unsafe if you have external code that may pollute the prototype chain. Several alternatives exist, such as {{jsxref("Object/hasOwn", "Object.hasOwn(element.dataset, 'keyname')")}}, or just checking if element.dataset.keyname !== undefined.When the attribute is set, its value is always converted to a string.
For example: element.dataset.example = null is
converted into data-example="null".
To remove an attribute, you can use the delete operator: delete element.dataset.keyname.
<div id="user" data-id="1234567890" data-user="carinaanand" data-date-of-birth>
Carina Anand
</div>
const el = document.querySelector("#user");
// el.id === 'user'
// el.dataset.id === '1234567890'
// el.dataset.user === 'carinaanand'
// el.dataset.dateOfBirth === ''
// set a data attribute
el.dataset.dateOfBirth = "1960-10-03";
// Result on JS: el.dataset.dateOfBirth === '1960-10-03'
// Result on HTML: <div id="user" data-id="1234567890" data-user="carinaanand" data-date-of-birth="1960-10-03">Carina Anand</div>
delete el.dataset.dateOfBirth;
// Result on JS: el.dataset.dateOfBirth === undefined
// Result on HTML: <div id="user" data-id="1234567890" data-user="carinaanand">Carina Anand</div>
if (el.dataset.someDataAttr === undefined) {
el.dataset.someDataAttr = "mydata";
// Result on JS: 'someDataAttr' in el.dataset === true
// Result on HTML: <div id="user" data-id="1234567890" data-user="carinaanand" data-some-data-attr="mydata">Carina Anand</div>
}
{{Specifications}}
{{Compat}}
data-* class
of global attributes