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>

Updated on: 25-Jun-2020

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements