How to store data to DOM?

Storing data in the DOM means keeping data associated with HTML elements or in JavaScript variables that can be accessed later. This is useful for maintaining application state, form data, or temporary values without making server requests.

In vanilla JavaScript, we can store data in objects or use element properties. jQuery provides the data() method for attaching data to specific DOM elements. Let's explore both approaches.

Using JavaScript Objects to Store Data

The most common approach is creating JavaScript objects to hold data. This keeps information organized and easily accessible.

Syntax

let dataObj = {
   property1: "",
   property2: ""
}
dataObj.property1 = value;

Example

Here's a form that stores user input in a JavaScript object:

<html>
<body>
   <h3>Store Form Data in JavaScript Object</h3>
   <form>
      <label for="fname">First name:</label><br>
      <input type="text" id="fname" name="fname"><br><br>
      <label for="lname">Last name:</label><br>
      <input type="text" id="lname" name="lname"><br><br>
   </form>
   <button onclick="storeData()">Store Data</button>
   <button onclick="displayData()">Show Stored Data</button>
   <div id="output"></div>

   <script>
      // Object to store form data
      let userData = {
         firstName: "",
         lastName: ""
      };

      function storeData() {
         userData.firstName = document.getElementById("fname").value;
         userData.lastName = document.getElementById("lname").value;
         document.getElementById("output").innerHTML = "<p style='color: green;'>Data stored successfully!</p>";
      }

      function displayData() {
         document.getElementById("output").innerHTML = 
            "<p><strong>Stored Data:</strong></p>" +
            "<p>First Name: " + userData.firstName + "</p>" +
            "<p>Last Name: " + userData.lastName + "</p>";
      }
   </script>
</body>
</html>

Using jQuery's data() Method

jQuery's data() method allows you to attach data directly to DOM elements. This creates a key-value store associated with specific elements.

Syntax

// Store data
$("selector").data("key", value);

// Retrieve data
var value = $("selector").data("key");

Example

This example shows how to store data using jQuery's data API:

<html>
<head>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
</head>
<body>
   <h3>Store Data with jQuery data() Method</h3>
   <form>
      <label for="email">Email:</label><br>
      <input type="email" id="email" name="email"><br><br>
      <label for="password">Password:</label><br>
      <input type="password" id="password" name="password"><br><br>
   </form>
   <button onclick="storeWithJQuery()">Store Data</button>
   <button onclick="getWithJQuery()">Show Stored Data</button>
   <div id="result"></div>

   <script>
      function storeWithJQuery() {
         var emailValue = $("#email").val();
         var passwordValue = $("#password").val();
         
         // Store data in the respective elements
         $("#email").data("userEmail", emailValue);
         $("#password").data("userPassword", passwordValue);
         
         $("#result").html("<p style='color: green;'>Data stored in DOM elements!</p>");
      }

      function getWithJQuery() {
         var storedEmail = $("#email").data("userEmail");
         var storedPassword = $("#password").data("userPassword");
         
         $("#result").html(
            "<p><strong>Retrieved Data:</strong></p>" +
            "<p>Email: " + storedEmail + "</p>" +
            "<p>Password: " + storedPassword + "</p>"
         );
      }
   </script>
</body>
</html>

Comparison of Methods

Method Storage Location Pros Cons
JavaScript Objects Memory variables Fast access, simple syntax Lost on page refresh
jQuery data() DOM elements Tied to specific elements Requires jQuery library

Best Practices

While storing data in the DOM is useful for temporary values, consider these alternatives for persistent data:

  • localStorage - Survives browser sessions
  • sessionStorage - Survives page refreshes within the same tab
  • Server-side storage - For data that needs to persist across devices

Conclusion

DOM data storage is effective for temporary application state. Use JavaScript objects for simple data organization, and jQuery's data() method when you need to associate data with specific elements. For persistent data, consider browser storage APIs or server-side solutions.

Updated on: 2026-03-15T23:19:01+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements