I was learning JS From W3 schools and encounter this program which checks the attribute length. I later modified it to display name of attribute that an element has. Here is the code
function myFunction() {
for (var counter = 0; counter < 2; counter++)
var a = document.getElementById("myBtn").attributes[counter].name;
document.getElementById("output").innerHTML = a;
}
<html>
<body>
<p>Click the button to see how many attributes the button element have.</p>
<p id="output">
</p>
<button id="myBtn" onclick="myFunction()">Try it</button>
</body>
</html>
I expected O/P to be as :
id onclick
But for some reason it only display the last attribute that an element holds in this case its onclick if it were class or something else it would have displayed that.
Whats the reason for that ?