If this parameter is omitted, **all** custom data previously attached to the selected element(s) will be removed. | --- ## Code Examples ### Example 1: Removing a Specific Data Key In this example, we attach a data property named `"greeting"` to a `
` element and then remove it when a button is clicked.
```html
``` ### Example 2: Removing All Stored Data If you call `.removeData()` without passing any arguments, jQuery will clear all custom data keys currently stored on the selected element. ```javascript // Attach multiple data properties $("div").data("role", "admin"); $("div").data("id", 1024); // Remove all data associated with the div $("div").removeData(); console.log($("div").data("role")); // Output: undefined console.log($("div").data("id")); // Output: undefined ``` --- ## Important Considerations 1. **HTML5 `data-*` Attributes:** The `.removeData()` method only removes data stored in jQuery's internal data cache. It **does not** remove physical `data-*` attributes from the HTML markup. If you query the data again after calling `.removeData()`, and a matching HTML5 `data-*` attribute exists on the element, jQuery may read it back in. To completely remove an HTML5 attribute, use jQuery's `.removeAttr("data-...")` instead. 2. **Multiple Keys:** Starting from jQuery 1.7, you can remove multiple data keys at once by passing them as a space-separated string (e.g., `$(selector).removeData("key1 key2")`) or as an array of strings (e.g., `$(selector).removeData(["key1", "key2"])`).
This is a div element.
``` ### Example 2: Removing All Stored Data If you call `.removeData()` without passing any arguments, jQuery will clear all custom data keys currently stored on the selected element. ```javascript // Attach multiple data properties $("div").data("role", "admin"); $("div").data("id", 1024); // Remove all data associated with the div $("div").removeData(); console.log($("div").data("role")); // Output: undefined console.log($("div").data("id")); // Output: undefined ``` --- ## Important Considerations 1. **HTML5 `data-*` Attributes:** The `.removeData()` method only removes data stored in jQuery's internal data cache. It **does not** remove physical `data-*` attributes from the HTML markup. If you query the data again after calling `.removeData()`, and a matching HTML5 `data-*` attribute exists on the element, jQuery may read it back in. To completely remove an HTML5 attribute, use jQuery's `.removeAttr("data-...")` instead. 2. **Multiple Keys:** Starting from jQuery 1.7, you can remove multiple data keys at once by passing them as a space-separated string (e.g., `$(selector).removeData("key1 key2")`) or as an array of strings (e.g., `$(selector).removeData(["key1", "key2"])`).
YouTip