The attributes property in HTML DOM returns the group of node attributes specified by NamedNodeMap objects. The NamedNodeMap object represents the collection of attribute objects and can be accessed by index number. The index number starts at 0.
Syntax:
node.attributes
Return Value: It returns the NamedNodeMap object which is the collection of nodes.
Note: In Internet Explorer 8 and earlier versions, the attributes property will return a collection of all possible attributes for an element that can result in higher value than expected.
Example 1:
html
<!DOCTYPE html>
< html >
< head >
< title >
HTML DOM attributes Property
</ title >
</ head >
< body >
< img id = "GFG" src =
< br >
< button onclick = "myGeeks()" >
DOM attributes property
</ button >
< p id = "demo" ></ p >
< script >
function myGeeks() {
// It returns the number of nodes
var x = document.getElementById("GFG").attributes.length;
// Display the number of nodes
document.getElementById("demo").innerHTML = x;
}
</ script >
</ body >
</ html >
|
Output:
Example 2:
html
<!DOCTYPE html>
< html >
< head >
< title >
HTML DOM attributes Property
</ title >
</ head >
< body >
< h2 >
HTML DOM attributes Property
</ h2 >
< button id = "GFG" onclick = "myGeeks()" >
Click Here!
</ button >
< br >
< br >
< span >
Button element attributes:
</ span >
< span id = "sudo" ></ span >
< script >
function myGeeks() {
// It returns the number of nodes
var gfg =
document.getElementById("GFG").attributes.length;
// Display the number of nodes
document.getElementById("sudo").innerHTML = gfg;
}
</ script >
</ body >
</ html >
|
Output:
Supported browsers The browser supported by DOM attributes property are listed below:
- Google Chrome 1 and above
- Edge 12 and above
- Internet Explorer 5.5 and above
- Firefox 1 and above
- Opera 8 and above
- Apple Safari 1 and above
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
13 Jun, 2023
Like Article
Save Article