Description: Remove a previously-stored piece of data.
nameA string naming the piece of data to delete.
The .removeData()
method allows us to remove values that were previously set using .data()
. When called with the name of a key, .removeData()
deletes that particular value; when called with no arguments, all values are removed.
<!DOCTYPE html>
<html>
<head>
<style>
div { margin:2px; color:blue; }
span { color:red; }
</style>
<script src="https://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>
$("span:eq(0)").text("" + $("div").data("test1"));
$("div").data("test1", "VALUE-1");
$("div").data("test2", "VALUE-2");
$("span:eq(1)").text("" + $("div").data("test1"));
$("div").removeData("test1");
$("span:eq(2)").text("" + $("div").data("test1"));
$("span:eq(3)").text("" + $("div").data("test2"));
</script>
</body>
</html>