I have the following in my source HTML:
<li><a href="#" data-value="01">Test</a></li>
I want to get the value of the data attribute so I am using the following:
var abc = $(this).data('value');
This works okay BUT not if there is a leading zero. For example the above placed the value "1" into abc.
Is there a way that I can get it not to convert the "01" into "1" ?
click(function() { $("#ask_user"). hide(); $. ajax({ type: "post", url: '/nearest_banks/radius', contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { $("#allowed"). show(); var str = ""; $("#places").
The String() method converts a value to a string.
this.dataset.value; // Or old school this.getAttribute('data-value');
const a = document.querySelector("a"); console.log('Using getAttribute, old school: ', a.getAttribute('data-value')); console.log('Using dataset, conforms to data attributes: ', a.dataset.value);
<ul> <li><a href="#" data-value="01">Test</a></li> </ul>
Thanks to @MaksymMelnyk for the heads up on dataset
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With