What happens to the data stored for a given element using the jQuery.data() function after that element is removed or replaced? In tests it appears this data is no longer available after the element it was attached to is removed.
What happens to the data stored for a given element using the jQuery.data() function after that element is removed or replaced? In tests it appears this data is no longer available after the element it was attached to is removed.
Share Improve this question edited Nov 8, 2011 at 1:42 TJ Seabrooks 20.8k6 gold badges34 silver badges30 bronze badges asked Dec 4, 2010 at 11:32 toofamousnametoofamousname 1512 bronze badges 1- 2 Hey, good question! +1. Nothing in the jQuery docs: api.jquery./jQuery.data one would probably have to look into the source code. – Pekka Commented Dec 4, 2010 at 11:33
1 Answer
Reset to default 16Yes, when an element is removed by .remove()
or .empty()
on a parent (or .html()
on a parent), the data (stored in the jQuery.cache
object) is cleaned up as well.
You can see in the source:
- Here's where it happens for
.remove()
- Here's where it happens for
.empty()
- Here's where it happens for
.html()
In all these cases, the jQuery.cleanData()
function does the work.
In case others finding this are looking for ways to directly remove data, there are functions for this, just not monly used directly: .removeData()
and $.removeData()
. They behave like .data()
and $.data()
in usage - without the data key (name parameter) they'll clear all data values for the element.