0

Here is my code:

$(function(){
  $("div").data({"one": 1, "two":2});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>what ever</div>

After executing the code, I expect to see these two attributes to the element:

<div data-one="1" data-two="2">what ever</div>

But I don't see any:

enter image description here

Well why .data doesn't add those attributes? And how can I do that?

Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • 3
    [javascript - Where is jQuery.data() stored?](https://stackoverflow.com/questions/5821520/where-is-jquery-data-stored) – Andreas May 12 '18 at 09:11
  • _"And how can I do that?"_ - Why would you want to do this in the first place? – Andreas May 12 '18 at 09:15
  • For no good reason: `$("div").attr("data-one", 1).attr("data-two", 2);` There's no need for this - just always use `data-` and `.data()` for data. – freedomn-m May 12 '18 at 09:17
  • Possible duplicate of [Where is jQuery.data() stored?](https://stackoverflow.com/questions/5821520/where-is-jquery-data-stored) – freedomn-m May 12 '18 at 09:18

1 Answers1

0

In jQuery all the data elements are store in $.cache. If you want to reflect in your DOM. Then use $("div").data({property:value}).attr({property:value}):

Naveen DA
  • 4,148
  • 4
  • 38
  • 57