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 function. I'm just trying to understand a little more about how Javascript handles Numbers and Strings. This kind of caught me off guard.
<a data-value="2.0">2.0</a>
<a data-value="2.5">2.5</a>
$("a").click(function() {
alert(typeof $(this).data( "value"));
});
0
are always pretended to be strings. Maybe because the last0
is irrelevant and so interpreted as text. For example:2.5
is number and2.50
is string. – Ageold.data
method will attempt to convert whatever is in thedata-*
attribute - if you want it as the raw string value, use.attr("data-*")
– Stagnate