get wrong value in data attribute jquery

The first time that you use .data() to access a data-* attribute, the value of that attribute is cached internally by jQuery, and .data() uses the cache from then on. Updating the attribute with .attr() does not update the cache, you need to use .data() to update it. That’s why you need to use

$(element).data('location', count);

to update it.

Leave a Comment