Why is the value of data-value=\"2.0\"
cast to a String and the value of data-value=\"2.5\"
cast to a Number?
I can handle this fine within my func
I think it's more a question about how jquery identifies numbers. If you use alert(typeof(this.getAttribute("data-value")));
, it spits out that it's a string both times (which is expected). So far as I know, everything in an html attribute is considered a string, just different libraries may have default behavior that interprets them differently.
Also, a shorthand way to get a number would be something along the lines of...
var someNumber = +$(someElt).data("value");
the + will cause type coercion if the returned value is a string, or leave it alone if it's already a number.
Those values are simply strings to vanilla javascript; it's not attempting any conversion on its own.
[...document.querySelectorAll("a")].forEach(a =>
console.log("type: %s, value: %o",
typeof a.dataset.value,
a.dataset.value)
);
<a data-value="2.0">2.0</a>
<a data-value="2.5">2.5</a>
jQuery, on the other hand, tries to determine and convert to the appropriate type when accessing data attributes via data(). It's (arguably) an issue with their implementation. Their documentation (emphasis mine) actually addresses this:
Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null). A value is only converted to a number if doing so doesn't change the value's representation. For example, "1E02" and "100.000" are equivalent as numbers (numeric value 100) but converting them would alter their representation so they are left as strings. The string value "100" is converted to the number 100.
See also HTMLElement.dataset
By default anything parsed from an attribute will be a string, you will need to convert it to a number if you need to use it as a number. The best way I've been able to handle this is by wrapping it with the Number function (native javascript)
var number = Number($(this).data("value"));
As tymeJV mentioned, this looks like an issue with how jquery handles autoconversion. If you use "2", it gives a number as well, so I'm guessing its just a weird edge case in how they handle things. I would encourage just using .attr('xxx') and parsing out your data to its known type.
Looking through jQuery's source code, i have identified the reason.
It will parse it as a number, if, and only if, doing so does not change the string. Source: https://github.com/jquery/jquery/blob/master/src/data.js#L36
(+"2.0") + "" === "2" // !== the original string
(+"2.1") + "" === "2.1" // == the original string
It appears to be treating that ".0" extension of the number like a string.
If all of your data values are going to be coming in in that format, just whip a parseFloat()
on those suckers like this:
$("a").click(function() {
alert(typeof parseFloat($(this).data( "value")));
});
That way it won't matter if they are strings or numbers, they will always be treated like numbers(decimals intact).