Esta traducción está incompleta. Por favor, ayuda a traducir este artículo del inglés.

La propieda dataset en HTMLElement proporciona una interfaz lectura/escritura para obtener todos los custom data attributes (data-*) de cada uno de los elementos. Esta disponible el acceso en HTML y en el DOM.  Dentro del map of DOMString, una entrada por cada custom data attribute.  Tomar en cuenta que la propiedad dataset puede leerse a si mismo, pero no modificarse directamente.  Instead, all writes must be to the individual properties within the dataset, which in turn represent the data attributes.   Note also that an HTML data-attribute and its corresponding DOM dataset.property do not share the same name, but they are always similar:

  • The name of a custom data attribute in HTML begins with data-. It must contain only letters, numbers and the following characters: dash (-), dot (.), colon (:), underscore (_) -- but NOT any ASCII capital letters (A to Z).
  • The name of a custom data attribute in JavaScript is the name of the same HTML attribute but in camelCase and with no dashes, dots, etc.

In addition to the information below, you'll find a how-to guide for using HTML data attributes in our article Using data attributes.

Name conversion

dash-style to camelCase: A custom data attribute name is transformed to a key for the DOMStringMap entry with the following rules

  • the prefix data- is removed (including the dash);
  • for any dash (U+002D) followed by an ASCII lowercase letter a to z, the dash is removed and the letter is transformed into its uppercase counterpart;
  • other characters (including other dashes) are left unchanged.

camelCase to dash-style: The opposite transformation, that maps a key to an attribute name, uses the following rules:

  • Restriction: A dash must not be immediately followed by an ASCII lowercase letter a to z (before the transformation);
  • a prefix data- is added;
  • any ASCII uppercase letter A to Z is transformed into a dash followed by its lowercase counterpart;
  • other characters are left unchanged.

The restriction in the rules above ensures that the two transformations are the inverse one of the other.

For example, the attribute named data-abc-def corresponds to the key abcDef.

Accessing values

  • Attributes can be set and read by using the camelCase name (the key) like an object property of the dataset, as in element.dataset.keyname
  • Attributes can also be set and read using the object-properties bracket-syntax, as in element.dataset[keyname]
  • The in operator can be used to check whether a given atttribute exists.

 

Setting values

  • When an attribute is set, it's value is always converted into a string. For example null is converted into the string "null".
  • When you want to remove an attribute, you can use the delete operator.

Sintaxis

  • string = element.dataset.camelCasedName;
  • element.dataset.camelCasedName = string;

  • string = element.dataset[camelCasedName];
  • element.dataset[camelCasedName] = string;

  • Custom data attributes can also be set directly on HTML elements, but attribute names must use the data- syntax above.

Ejemplos

<div id="user" data-id="1234567890" data-user="johndoe" data-date-of-birth>John Doe</div>
const el = document.querySelector('#user');

// el.id == 'user'
// el.dataset.id === '1234567890'
// el.dataset.user === 'johndoe'
// el.dataset.dateOfBirth === ''

// set the data attribute
el.dataset.dateOfBirth = '1960-10-03'; 
// Result: el.dataset.dateOfBirth === 1960-10-03

delete el.dataset.dateOfBirth;
// Result: el.dataset.dateOfBirth === undefined

// 'someDataAttr' in el.dataset === false
el.dataset.someDataAttr = 'mydata';
// Result: 'someDataAttr' in el.dataset === true

Especificaciones

Especificación Estado Comentario
HTML Living Standard
La definición de 'HTMLElement.dataset' en esta especificación.
Living Standard No change from latest snapshot, HTML 5.1
HTML 5.1
La definición de 'HTMLElement.dataset' en esta especificación.
Recommendation Snapshot of HTML Living Standard, no change from HTML5
HTML5
La definición de 'HTMLElement.dataset' en esta especificación.
Recommendation Snapshot of  HTML Living Standard, initial definition.

Compatibilidad en navegadores

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Soporte básicoChrome Soporte completo 8Edge Soporte completo SiFirefox Soporte completo 6IE Soporte completo 11Opera Soporte completo 11Safari ? WebView Android ? Chrome Android ? Edge Mobile Soporte completo SiFirefox Android Soporte completo 6Opera Android ? Safari iOS ? Samsung Internet Android ?

Leyenda

Soporte completo  
Soporte completo
Compatibility unknown  
Compatibility unknown

Ver también

Etiquetas y colaboradores del documento

Colaboradores en esta página: pipepico, AlePerez92
Última actualización por: pipepico,