Attribute length Property - Javascript DOM

Javascript examples for DOM:Attribute

Description

The length property returns the number of nodes in a NamedNodeMap object.

This property is read-only.

Return Value

A Number, representing the number of attribute nodes in the nodemap

The following code shows how to get the number of attributes of a <button> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.example {/*from  www. jav a2  s .  c  om*/
    color: red;
    padding: 5px;
    width: 150px;
    font-size: 15px;
}
</style>
</head>
<body>

<button id="myBtn" onclick="myFunction()" class="example">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var txt = "";
    var x = document.getElementById("myBtn").attributes;

    var i;
    for (i = 0; i < x.length; i++) {
        txt += "Attribute name: " + x[i].name + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

Related Tutorials