Attribute length Property - Loop through all attributes of a <button> element and output the name of each attribute: - Javascript DOM

Javascript examples for DOM:Attribute

Description

Attribute length Property - Loop through all attributes of a <button> element and output the name of each attribute:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.example {/* w  w w  . jav  a  2  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