28

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" ?

4
  • possible duplicate of HTML data and a string that looks like a number Commented Apr 4, 2012 at 16:41
  • I'm not aware of data-value being a correct attribute of an <a> element. Wouldn't it be better to use the rel attribute? Commented Apr 4, 2012 at 16:48
  • 1
    @JakeJ - See ejohn.org/blog/html-5-data-attributes Commented Apr 4, 2012 at 17:21
  • @j08691 Ah so it's HTML5 it seems by a quick scan. However if it was me developing the site I would stay away from most or all HTML5 features (unless there was a graceful fallback) as to include all old browsers. Thanks for the link, learn something new every day :) +1 Commented Apr 5, 2012 at 9:11

7 Answers 7

53
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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, Florian, I usually prefer to answer with straight JS, if possible. Though, if the OP is already using jQuery, attr may be a better option, since getAttribute can be buggy across browsers in some edge cases, like when the attribute is one that is used by DOM, such as obj.disabled.
Well, you can use this.dataValue then :) I don't know of any bug around this.
@FlorianMargaine: I had never heard of that. It doesn't seem to work jsfiddle.net/uV9dv
@JuanMendes, @FlorianMargaine this.dataset.value will have that value.
17

You can use the attr() operator of jQuery:

var abc = $(this).attr('data-value');

Comments

14

From the jQuery data() documentation, it appears that you have to use .attr() to prevent the automatic type conversion:

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null) otherwise it is left as a string. To retrieve the value's attribute as a string without any attempt to convert it, use the attr() method. When the data attribute is an object (starts with '{') or array (starts with '[') then jQuery.parseJSON is used to parse the string; it must follow valid JSON syntax including quoted property names. The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery).

So, not to duplicate other answers, but rather to reinforce, the way to access the attribute without doing type conversion is:

var abc = $(this).attr('data-value');

Comments

6
$(this).attr('data-value');

This will return the string value with the leading 0.

Comments

3

have you tried?:

$(this).attr('data-value');

or

$(this).attr('data-value').toString();

1 Comment

$.attr doesn't need toString, it already returns a string, even if it didn't, calling toString would not bring back the original string if it had already been converted to a number.
0
<article id="electric-cars" data-columns="3"data-index-number="12314" data-parent="cars">

</article>


const article = document.querySelector('#electric-cars');
 
article.dataset.columns // "3"
article.dataset.index-number // "12314"
article.dataset.parent // "cars"

2 Comments

Welcome to StackOverflow! While this answer may provide a solution for the OP's needings, please, add an explanation in order to make it more understandable, not only for the OP, but also for the whole community
please feel free to ask any questions regarding to that answer..! hope this will work..!
-1

Try :

this.data('value');

I think this is best way to get data attribute value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.