The removeData method
The removeData method allows us to remove values set using the data method.
Syntax
By calling a method with a key name, we delete the value stored in the object with the given key:
.removeData(key name);
We can also pass the method a list as an array or a string, where the key names are separated by spaces, to delete multiple values at once:
.removeData(list);
If we don't pass any parameters to the method, we will delete all the data:
.removeData();
Example
Let's use data to set the data for the p tag, passing in the key name 'test' and the value 21, and then use removeData to remove it again:
<p>text</p>
$('p').data('test', 21); // data is written console.log($('p').data('test')); // will output 21
$('p').removeData('test'); // data for 'test' removed console.log($('p').data('test')); // will output undefined
See also
-
method
data,
which allows you to write and read data in an element -
method
hasData,
which checks if an element contains data -
JavaScript method
removeAttribute,
which allows you to remove a given attribute from a tag