Ticket #8191 (closed bug: worksforme)
removeData() does not remove data-* attribute
| Reported by: | speednet | Owned by: | speednet |
|---|---|---|---|
| Priority: | low | Milestone: | 1.next |
| Component: | data | Version: | 1.5 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
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.
Change History
comment:1 Changed 2 years ago by addyosmani
- Owner set to speednet
- Priority changed from undecided to low
- Status changed from new to pending
comment:2 Changed 2 years ago by jitter
- Status changed from pending to closed
- Resolution set to worksforme
- Component changed from unfiled to data
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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

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!.