0xDECAFBAD 0xDECAFBAD - 1 year ago 98
Javascript Question

DOMException: Invalid property name on dataset

I receive "Uncaught DOMException: Failed to set the 'child-count' property on 'DOMStringMap': 'child-count' is not a valid property name." when the following code is executed:

elem.dataset['child-count'] = "test";

where elem is a HTMLElement.

Thought it is legit to use the '-' character within data-attribute names or am I missing something else?

Answer Source

The dataset properties are camel-cased: elem.dataset.childCount. They are implicitly transformed to hyphenated attribute names (data-child-count in your case).