HTML5 data-* attribute type casting strings and numbers

JavascriptWeb DevelopmentFront End Scripts

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>
raja
Published on 05-Apr-2018 15:54:54
Advertisements