How to force string value in html 5 'data' attributes

how do you force the text value in the html 5 data attribute when accessing it in js?

I have a global application with a similar html line:

<div class="slider" data-max="125" data-cost="10000" data-costtext="10.000"> 

the. in the text, the cost value is accurate for some languages. When accessing this value through jQuery js

 $("div.slider","body").data("costtext") 

you will return 10 .

Is re-writing my only option ?, for example:

 <div class="slider" data-obj='{"max":125,"cost":10000,"costtext":"10.000"}'> 
+7
source share
1 answer

Use attr("data-x") to access the value and you will have String .

jQuery tries to infer a type and automatically convert it from String when using data() .

Each attempt is made to convert a string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null); otherwise, it remains as a string. To get the value attribute as a string without any attempt to convert it, use the attr() method.

Source

+15
source

All Articles