I need to store data (a string) on a certain HTML node.
This can be done via the dataset object
document.body.dataset.foo = 'bar';
Given the nature of JS however, node instances are also (just) objects, and thus you can also simply define a new property implicitly on the fly like so:
document.body.bar = 'foo';
Both methods fulfill the requirement of storing and then retrieving data of simple types.
I'm aware of the following differences:
dataset
writes the the information as attribute on the node (data-{key}="{value}"
)- by using the subobject (dataset) you are less likely to overwrite an existing property
In my case, I would prefer not to have the information written as data-attribute, so I lean towards the property approach. Most sources on the internet however want you to use the dataset method.
Am I missing something here? Are there any other particularities to consider?