-1

Im having an issue or possibly a expected result with JQuery's .data() method. Im unsure why when using the element selector the data values are not updated after running the data method. See the screenshot for an example of what im talking about.

enter image description here

Im unsure why when using the data(key,value) method, it updates the elements data with the expected output. However why is it that it doesnt updated the values html attribute? My knowledge in Javascript is still very limited, but i'd love to know what this is and if i should expect this.

Jaison Brooks
  • 5,816
  • 7
  • 43
  • 79

1 Answers1

1

jQuery reads all data-attributes on pageload. It then deals with changes by the .data()-function only in it's storage.

To update the "real" HTML-Attribute, you need to manipulate the content like so:

$('.selected a span:eq(3)').attr('data-value', 'newValue');
Dario
  • 582
  • 1
  • 5
  • 17
  • 1
    note however that doing so will not change what is already stored in jQuery's .data() – Kevin B Oct 18 '16 at 17:43
  • 1
    No, you would have to do changes on both, the attribute and the jQuery's .data(), to do so. – Dario Oct 18 '16 at 17:44
  • 1
    (to add to @KevinB's 100% correct comment) ...except when you are defining a *new* `data-` attribute that has not been set by `.data()`, since `data-*` values are used as defaults when no `.data()` has been set for some key. – apsillers Oct 18 '16 at 17:45
  • perfect, this is exactly what i needed to know. Thank you all – Jaison Brooks Oct 18 '16 at 17:47