1/* html */
2<a data-id="123">link</a>
3
4/* js */
5$(this).attr("data-id") // returns string "123"
6
7$(this).data("id") // returns number 123 (jQuery >= 1.4.3 only)
1var element = document.querySelector('.element');
2var dataAttribute = element.getAttribute('data-name');
3// replace "data-name" with your data attribute name
4
5console.log(dataAttribute);
1//get data-id attribute in plain Javascript
2var element = document.getElementById('myDivID');
3var dataID = element.getAttribute('data-id');
4
5//get data-id using jQuery
6var dataID = $('myDivID').data('data-id');
7
1Syntax:
2<data value=" "> Write your text here </data>
3Example:
4<body>
5 <h2>Example for HTML Data Tag</h2>
6 <p>Subjects Name:</p>
7 <ul>
8 <li> <data value="1000">Maths</data></li>
9 <li> <data value="1003">Science</data></li>
10 <li> <data value="1009">English</data></li>
11 </ul>
12 </body>
13
14
15
16
17
18
19
20
21
22
23
24
25