Computer >> Computer tutorials >  >> Programming >> Javascript

HTML5 data-* attribute type casting strings and numbers


For data-attribute typecasting of Numbers and String, use −

[...document.querySelectorAll("a")].forEach(a =>
   console.log("type: %s, value: %o", typeof a.dataset.value, a.dataset.value)
);

The above is for the following data-attributes −

<a data-value = "6.0">6.0</a>
<a data-value = "6.5">6.5</a>