CKTIS / HTML
data-*: application-specific attributes
Store string state in markup without treating it as trusted or secret.
Version: HTML Living Standard
Syntax
<button data-item-count="2">Add</button>Custom data-* attributes store information for the page’s own application. Supply a name after the prefix and prefer lowercase hyphenated names. The dataset interface maps a name such as data-item-count to the camel-cased property itemCount. Reading it returns text, even if the attribute contains digits.
Convert and validate values before arithmetic or business decisions. Setting a dataset property updates the corresponding attribute, and assigned values become strings. This data is visible to users and mutable in the browser: it is not a place for secrets or server authorization. Use an existing standard attribute when it already expresses the meaning.
The button starts with a count of two. Its handler explicitly converts the string to a number, increments it and writes it back. The result checks both the dataset value and the synchronized attribute, avoiding accidental string concatenation.
Examples
<!doctype html><meta charset="utf-8">
<button id="add" data-item-count="2">Add</button><output id="result"></output><script>
const button = document.querySelector('#add'), beforeType = typeof button.dataset.itemCount;
button.addEventListener('click', () => { button.dataset.itemCount = String(Number(button.dataset.itemCount) + 1); });
button.click();
document.querySelector('#result').textContent = 'beforeType=' + beforeType + '; after=' + button.dataset.itemCount + '; attribute=' + button.getAttribute('data-item-count');
</script>Result
beforeType=string; after=3; attribute=3
The conversion is explicit; the attribute itself does not make its value numeric.