jQuery.removeData()

jQuery

jQuery.removeData()


jQuery.removeData( element [, name ] ) Returns: jQuery

Description: Remove a previously-stored piece of data.

  • version added: 1.2.3jQuery.removeData( element [, name ] )

    • element
      Type: Element
      A DOM element from which to remove data.
    • name
      Type: String
      A string naming the piece of data to remove.

Note: This is a low-level method, you should probably use .removeData() instead.

The jQuery.removeData() method allows us to remove values that were previously set using jQuery.data(). When called with the name of a key, jQuery.removeData() deletes that particular value; when called with no arguments, all values are removed.

Example:

Set a data store for 2 names then remove one of them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
                                  
<!DOCTYPE html>
<html>
<head>
<style>
div { margin:2px; color:blue; }
span { color:red; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div>value1 before creation: <span></span></div>
<div>value1 after creation: <span></span></div>
<div>value1 after removal: <span></span></div>
<div>value2 after removal: <span></span></div>
<script>
var div = $("div")[0];
$("span:eq(0)").text("" + $("div").data("test1"));
jQuery.data(div, "test1", "VALUE-1");
jQuery.data(div, "test2", "VALUE-2");
$("span:eq(1)").text("" + jQuery.data(div, "test1"));
jQuery.removeData(div, "test1");
$("span:eq(2)").text("" + jQuery.data(div, "test1"));
$("span:eq(3)").text("" + jQuery.data(div, "test2"));</script>
</body>
</html>