How to get the data attributes of an element using JavaScript ?
Given an HTML document and the task is to select the data attributes of an element using JavaScript. There are few methods to solve this problem which are discussed below:
Approach:
- First, select the element which is having data attributes.
- We can either use dataset property to get access to the data attributes or use .getAttribute() method to select them by specifically typing their names.
Example 1: This example uses dataset property to get the data attributes of an element.
<!DOCTYPE HTML> <html> <head> <title> How to get the data attributes of an element using JavaScript ? </title> </head> <body align = "center"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <span data-typeId="123" data-name = "name" data-points="10" data-important="true" id="span"> This is the Element. </span> <br><br> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "font-size: 20px; font-weight: bold; color:green;"> </p> <script> var el_up = document.getElementById('GFG_UP'); var el_down = document.getElementById('GFG_DOWN'); var input = document.getElementById('span'); el_up.innerHTML = "Click on the button to get " + "the data attributes of element."; function GFG_Fun() { var jsonData = JSON.stringify({ id: parseInt(input.dataset.typeid), points: parseInt(input.dataset.points), important: input.dataset.important, dataName: input.dataset.name }); el_down.innerHTML = jsonData; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example uses .getAttribute() method to get the data attributes of an element.
<!DOCTYPE HTML> <html> <head> <title> How to get the data attributes of an element using JavaScript ? </title> </head> <body align = "center"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <span data-typeId="123" data-name = "name" data-points="10" data-important="true" id="span"> This is the Element. </span> <br><br> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "font-size: 20px; font-weight: bold; color:green;"> </p> <script> var el_up = document.getElementById('GFG_UP'); var el_down = document.getElementById('GFG_DOWN'); var input = document.getElementById('span'); el_up.innerHTML = "Click on the button to get " + "the data attributes of element."; function GFG_Fun() { var jsonData = JSON.stringify({ id: parseInt(input.getAttribute('data-typeId')), points: parseInt(input.getAttribute('data-points')), important: input.getAttribute('data-important'), dataName: input.getAttribute('data-name') }); el_down.innerHTML = jsonData; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to append data to <div> element using JavaScript ?
- HTML | data-* Attributes
- Remove blank attributes from a JavaScript Object
- JavaScript Course | Data Types in JavaScript
- How to pass variables and data from PHP to JavaScript ?
- How to get form data using JavaScript/jQuery?
- How to set the value of a select box element using JavaScript?
- How to add a class to DOM element in JavaScript?
- How to get the type of DOM element using JavaScript?
- How to get the child element of a parent using JavaScript ?
- JavaScript | How to add an element to a JSON object?
- How to append an element in an array in JavaScript?
- Print the content of a div element using JavaScript
- Find the min/max element of an Array using JavaScript
- How to swap key and value of JSON element using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


