To get attributes of a div using JavaScript, get reference to the div element, and read the attributes property of the div element.
In the following example, we have div with id "myDiv", and we shall get the attributes of this div in JavaScript using attributes property.
Get Attributes of Div using JavaScript
Try this program online, and open Developer Tools. Open Console window.
Click on the 'Click Me' button in our HTML page. The following shall be displayed in the console window.
attributes property returns NameNodeMap object with the attributes in the div element, where the attributes can be accessed using name of the attribute or the index.
Let us print the class attribute to console.
Example – Print Attributes in For Loop
Get Attributes of Div using JavaScript
Try online, open Console window in Developer tools, and click on the Click Me button in the HTML page.
First read the answer fully, then try to explain it in your own words. After that, open a few related questions and compare the concepts. This method helps you remember the topic for a longer time and improves exam preparation.