Phillip Senn
Phillip Senn

Reputation: 47595

removeData causing the value to revert to the value of

According to the documentation, .removeData() will cause the value of the property being removed to revert to the value of the data attribute of the same name in the DOM.

Q: What does that mean?

Upvotes: 0

Views: 79

Answers (2)

Šime Vidas
Šime Vidas

Reputation: 185913

Given this (initial) HTML source code:

<div data-x="1"></div>

Consider this code:

div.data('x'); // returns 1

div.data('x', 2);

div.data('x'); // returns 2

div.removeData('x');

div.data('x'); // returns 1

(where div refers to the DIV object in the DOM.)

Live demo: http://jsfiddle.net/DyGBB/

So, data('x') will initially return the value of the data-x HTML attribute of that DIV. However, if you programmatically set this same data-property like so: data('x', 2), then data('x') no longer returns the value of the data-x HTML attribute, but the value that you stored using data('x', ...).

If you now execute removeData('x'), the programmatically set value will be deleted, so that data('x') will once again return the value of the data-x HTML attribute.

Note that "HTML attributes" don't actually exist anymore once the page has loaded and the DOM has been built. However, DOM elements contain an attributes property which is an object that contains a corresponding property for each HTML attribute which was defined on the HTML element in the HTML source code.

Upvotes: 2

S. Albano
S. Albano

Reputation: 707

If your object corresponds to the following html element:

<div data-attname="some value"></div>

then the property of the javascript object called "attname" will revert to "some value" from whatever you had it set to.

Upvotes: 1

Related Questions