James
James

Reputation: 559

Accessing HTML5 data-* attribute after changing its value

I'm using a data-count attribute to count how many times a list element has been dropped into a list of targets. The element can only be dropped into each target once, but can be dropped into multiple targets.

The problem is that I need to keep track and display a count. I'm using the data attribute and running into the following problem.

On the drop, I check the initial count with

var original_count = member.data('count');

If the count is zero, I add a class to the original element, and update its data attribute with

member.attr('data-count', 1);

This works fine, I can see the change in the DOM after dropping it once. When I go to drop it a second time...

member.data('count');

Returns the initial value of zero, instead of the updated value of 1. If I change the count checker to

var original_count = member.attr('data-count');

It returns zero every drop. I just need to be able to access the updated value of an html5 data attribute. Going to RTFM and see what I missed, but any help would be appreciated.

Upvotes: 2

Views: 510

Answers (1)

JQuery caches the data values for faster access. Use data(name, newvalue) to set the values instead of attr.

Upvotes: 3

Related Questions