Al.G. Al.G. - 1 year ago 60
HTML Question

Get data attribute

In my html I have a


<span class="field" data-fullText="This is a span element">This is a</span>

And I want to get the
attribute. I tried these two ways, but they didn't work (the both return

$('.field').hover(function () {
console.log('using prop(): ' + $(this).prop('data-fullText'));
console.log('using data(): ' + $(this).data('fullText'));

Then I searched and found these questions: How to get the data-id attribute using jquery? and jquery cant get data attribute value.

The both's answers are
"Use .attr('data-sth') or .data('sth')"

I know that
is deprecated (in jquery-1.11.0, which I use), but, however, I tried it.

And it workded!

Can someone explain why?

Answer Source

You could use the .attr() function:


or if you lowercase the attribute name:

data-fulltext="This is a span element"

then you could use the .data() function:


The .data function expects and works only with lowercase attribute names