How to store data in the DOM ? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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. Syntax: $(selector).data(name) 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. HTML <!DOCTYPE HTML> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> </head> <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/> <br/> <button id="b1">Store entered data</button> <button id="b2">View data </button><br/><br/> <div id="display"></div> <script> $(document).ready(function(){ // We are setting user data to data tag $("#b1").click(function(){ var d=($("#data").val()); $("#data").data("userdata",d); }); //Displaying data after accessing from data tag $("#b2").click(function(){ $("#display").html($("#data").data("userdata")) ; }); }); </script> </body> </html> Output: data() method Comment More info F faizamu19 Follow Improve Article Tags : JQuery HTML-DOM jQuery-Selectors jQuery-Questions Explore jQuery Tutorial 8 min read Getting Started with jQuery 4 min read jQuery Introduction 7 min read jQuery Syntax 2 min read jQuery CDN 4 min read jQuery SelectorsJQuery Selectors 5 min read jQuery * Selector 1 min read jQuery #id Selector 1 min read jQuery .class Selector 1 min read jQuery EventsjQuery Events 4 min read jQuery bind() Method 2 min read jQuery blur() Method 1 min read jQuery change() Method 2 min read jQuery EffectsjQuery animate() Method 2 min read jQuery clearQueue() Method 2 min read jQuery delay() Method 2 min read jQuery HTML/CSSjQuery addClass() Method 2 min read jQuery after() Method 1 min read jQuery append() Method 2 min read jQuery TraversingjQuery | Traversing 4 min read jQuery add() method 1 min read jQuery addBack() Method 2 min read Like