JavaScript element.attributes Property
You are Here:
JavaScript element.attributes Property
The element.attributes
property gets a collection of all attribute nodes registered to the specified node.
The attributes is a key/value pair of strings that represents any information regarding that attribute.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Click Me</button>
<p id="point"></p>
<script>
var x = document.getElementById("point");
var elem = document.getElementsByTagName("button")[0];
function myFunction(){
x.innerHTML = elem.attributes.length;
}
</script>
</body>
</html>
Syntax
element.attributes
Return Values
Value | Explanation |
---|---|
Object | Returns a NamedNodeMap object containing the assigned attributes of the corresponding HTML element. |
Get All Attributes
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<button id="myBtn" onclick="myFunction()">Click Me</button>
<p id="point"></p>
<script>
var x = document.getElementById("point");
var elem = document.getElementById("myBtn");
var len = elem.attributes.length
var txt = "";
function myFunction(){
for(var i=0; i<len; i++)
txt += elem.attributes[i].name +"<br>";
x.innerHTML = txt;
}
</script>
</body>
</html>
Get All Attributes with Values
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<button id="myBtn" onclick="myFunction()">Click Me</button>
<p id="point"></p>
<script>
var x = document.getElementById("point");
var elem = document.getElementById("myBtn");
var len = elem.attributes.length
var txt = "";
function myFunction(){
for(var i=0; i<len; i++)
txt += elem.attributes[i].name +" = "+ elem.attributes[i].value +"<br>";
x.innerHTML = txt;
}
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.