Skip to content
Related Articles
Open in App
Not now

Related Articles

How to store data in the DOM ?

Improve Article
Save Article
Like Article
  • Last Updated : 18 May, 2021
Improve Article
Save Article
Like Article

In this article, we will learn to store data to DOM elements.

If we want to save some data in an HTML tag, we can use the jQuery .data() method to store data which can also be retrieved later by using the same method.



Attribute values:

  • name: It defines the name of the data to be stored and is an optional parameter.

Note: If no name is specified, the .data() method returns all stored data for that particular element. 

HTML code: Let us look at an example to understand how to store data to DOM using .data() method.


    <script src=
<body style="text-align:center;" id="body">
    <h2 style="color:green">GeeksforGeeks</h2>
    <p><b>Enter the value to store:</b></p>
    <input type="text" id="data" name="input"><br/>
    <button id="b1">Store entered data</button>
    <button id="b2">View data </button><br/><br/>
    <div id="display"></div>
     // We are setting user data to data tag 
         var d=($("#data").val());
      //Displaying data after accessing from data tag 
         $("#display").html($("#data").data("userdata")) ;


data() method

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!