How to store data to DOM ?
In this article we will learn how to store data to DOM. You can use data-* attribute to store data or information on DOM. The data-* attributes can be used to define our own custom data attributes. It is used to store custom data in private to the page or application.
There are mainly 2 parts of the Data Attributes:
- Attribute Name: The attribute name should be at least one character long, contain no capital letters and be prefixed with ‘data-‘.
- Attribute Value: Can be any string.
<div data-row = "value" data-column = "value" data-index = "value"> </div>
- Any Data stored in a DOM can be viewed in the source code(Developer Tool).
- You cannot use uppercase with data-* attributes
You can also use data() method in jQuery to store Data on DOM which is an inbuilt method in jQuery which is used to attach data or get data for the selected elements.