Javascript DOM HTML Node attributes Property get second attribute

Introduction

Get the name of a <button> element's second (index 1) attribute:

var x = document.getElementById("myBtn").attributes[1].name;

Click the button to display the name of the button's second attribute (index 1).

View in separate window

<!DOCTYPE html>
<html>
<body>
<button id="myBtn" onclick="myFunction()">Test</button>
<p id="demo"></p>

<script>
function myFunction() {/*from   w ww .  j  a  v  a 2s. c  o  m*/
  var x = document.getElementById("myBtn").attributes[1].name;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related