Get data-attribute jquery vs javascript

JavascriptJqueryCustom Data-Attribute

Javascript Problem Overview


I have a custom data-attribute set by default:

data-equipment="0"

If i change it with jquery using .data()

$(this).data("equipment", 10)

and then use the getAttribute()

this.getAttribute("data-equipment")

i get the old value (0) and not the new one (10). But if i use

$(this).data("equipment") i get the new value (10).

Is this supposed to work like this or am i missing something?

Thanks!

Javascript Solutions


Solution 1 - Javascript

.data() doesn't operate on data attributes but in internal jQuery cache. Initially if no cache record is found, the data is read from a corresponding data- attribute if one exists, but that is the end of their co-operation.

If it operated on attributes, it would be useless for its purpose because attribute values must be strings.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpedrotoView Question on Stackoverflow
Solution 1 - JavascriptEsailijaView Answer on Stackoverflow