• jQuery Video Tutorials

jQuery Misc removeData() Method



The jQuery removeData() method is used to remove data that was previously set using the data() method.

Syntax

$(selector).removeData(name)

Parameters

Here is the description of the above syntax −

  • name (optional): The name of the data to remove. If omitted, all data associated with the elements will be removed.

Example

In the following example, we are using the jQuery Misc removeData() method to remove previously attached data from a <div> element −

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#saveData").click(function(){
    $("#info").data("username", "Tutorialspoint");
    alert("Username saved: " + $("#info").data("username"));
  });
  
  $("#clearData").click(function(){
    $("#info").removeData("username");
    alert("Username after removal: " + $("#info").data("username"));
  });
});
</script>
</head>
<body>
<button id="saveData">Save Username</button><br>
<button id="clearData">Clear Username</button>
<p id="info"></p>
</body>
</html>

After executing, click the above buttons "save" and "clear" buttons.

jquery_ref_miscellaneous.htm
Advertisements