Javascript DOM HTML Attribute value Property

Introduction

The value property sets or gets the value of the attribute.

The following code gets the value of the <button> element's first attribute:

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

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

View in separate window

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

<script>
function myFunction() {//  www .j av a  2  s .  co m
  var btn = document.getElementsByTagName("BUTTON")[0];
  var x = btn.attributes[0].value;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

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




PreviousNext

Related