---
title: "Add or remove data using jQuery"  
description: "Add or remove data using jQuery"  
author: "Ethan Karla"  
published: 2021-07-28  
updated: 2023-11-28  
canonical: https://www.mindstick.com/forum/156633/add-or-remove-data-using-jquery  
category: "jquery"  
tags: ["jquery"]  
reading_time: 2 minutes  

---

# Add or remove data using jQuery

How to [add or remove](https://www.mindstick.com/forum/160320/how-you-can-check-if-an-element-exists-in-a-set-and-add-or-remove-elements-from-it) [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) using jQuery?

## Replies

### Reply by Aryan Kumar

Certainly! jQuery provides methods to [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) or [remove data](https://www.mindstick.com/forum/159778/what-is-the-delete-statement-in-sql-and-how-is-it-used-to-remove-data-from-a-table) to/from elements. Let's use the **data()** method to add and remove data from an element. Here's an example:

```plaintext
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Add or Remove Data with jQuery</title>
  <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>

  <div id="myDiv">This is a div element.</div>

  <script>
    $(document).ready(function(){
      // Adding data to the element
      $("#myDiv").data("key", "This is some additional data.");

      // Getting and displaying the added data
      var addedData = $("#myDiv").data("key");
      console.log("Added Data:", addedData);

      // Removing the added data
      $("#myDiv").removeData("key");

      // Attempting to get the removed data (will be undefined)
      var removedData = $("#myDiv").data("key");
      console.log("Removed Data:", removedData);
    });
  </script>

</body>
</html>
```

In this example:

- The **data()** method is used to add data with a specific key to the element with the ID "myDiv."
- The added data is then retrieved and displayed in the console.
- The **removeData()** method is used to remove the data associated with the specified key.
- An attempt is made to get the removed data, and it will be undefined.

Feel free to adapt this example based on your specific requirements and the type of element you are working with.


---

Original Source: https://www.mindstick.com/forum/156633/add-or-remove-data-using-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
