Skip to main content

Bug Tracker

Side navigation

#8191 closed bug (worksforme)

Opened February 06, 2011 10:00PM UTC

Closed February 06, 2011 11:40PM UTC

Last modified February 07, 2011 03:31AM UTC

removeData() does not remove data-* attribute

Reported by: speednet Owned by: speednet
Priority: low Milestone: 1.next
Component: data Version: 1.5
Keywords: Cc:
Blocked by: Blocking:
Description

If you set a data value with a data-* attribute on an element, and then in JavaScript code you use .removeData() to remove it, the attribute does not get removed as it should. If you query .data() right after removeData(), it will continue to return the original value in the data-* attribute.

Attachments (0)
Change History (3)

Changed February 06, 2011 10:28PM UTC by addyosmani comment:1

owner: → speednet
priority: undecidedlow
status: newpending

Thanks for submitting a ticket to the jQuery Bug Tracker. In order for us to assist you further, please provide a reduced test case on jsFiddle which reproduces the behavior you're experiencing. We'll evaluate the bug as soon as you've supplied this. Thanks!.

Changed February 06, 2011 11:40PM UTC by jitter comment:2

component: unfileddata
resolution: → worksforme
status: pendingclosed

I put together this live test case which shows everything is working as expected. The data that was set with .data() is correctly removed.

I think there may be a slight misunderstanding on what .removeData() does in the case of data-* attributes. The removeData() documentation reads:

The .removeData() method allows us to remove values that were previously set using .data().

and for data-* attributes reverts

to the value of the data attribute of the same name in the DOM

So .removeData() is never going to "remove" the actual data-* HTML attribute, which it also isn't supposed to as the original value wasn't set by .data().

I included some more code in the test case which you can uncomment and play around with to get a better understanding of how .data(), .removeData() and data-* attributes play together.

Changed February 07, 2011 03:31AM UTC by speednet comment:3

Wow, I missed that one in the docs, thanks very much for the review and great explanation.