jQuery removeData()

Introduction

Remove previously attached data from a <div> element:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#btn1").click(function(){
    $("div").data("greeting", "Hello World");
      document.getElementById("demo").innerHTML = 
        "Greeting is: " + $("div").data("greeting");
    });/*from w w  w. ja  v  a 2 s. com*/
  $("#btn2").click(function(){
    $("div").removeData("greeting");
      document.getElementById("demo").innerHTML = 
         "Greeting is: " + $("div").data("greeting");
  });
});
</script>
</head>
<body>

<p id="demo"></p>
<button id="btn1">Attach data to div element</button><br>
<button id="btn2">Remove data attached to div element</button>

<div></div>

</body>
</html>

The removeData() method removes data previously set with the data() method.

$(selector).removeData(name)
Parameter
Optional
Description
name

Optional.

the name of data to remove.
If no name is specified, it will remove all stored data



PreviousNext

Related