Javascript DOM HTML Attribute name Property get

Introduction

Get the name of an attribute:

var x = document.getElementsByTagName("BUTTON")[0].attributes[0].name;

Click the button to display the name of the button's first attribute.

View in separate window

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

<script>
function myFunction() {/*ww  w .j  ava2  s  .c o m*/
  var btn = document.getElementsByTagName("BUTTON")[0];
  var x = btn.attributes[0].name;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The name property returns the name of the attribute.

This property is read-only.

You can use the attr.value property to get the value of an attribute.

The name property returns a String representing the name of the attribute.




PreviousNext

Related