Javascript DOM HTML Attribute specified Property

Introduction

Find out if an attribute has been specified or not:

var x = document.getElementById("demo").attributes[0].specified;

Click the button find out if the button has an onclick attribute specified.

View in separate window

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

<p id="demo"></p>

<script>
function myFunction() {//from w  w w  .  j  a  v  a2  s.com
  var btn = document.getElementsByTagName("BUTTON")[0];
  var x = btn.getAttributeNode("onclick").specified;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The specified property returns true if the attribute is specified or if the attribute has been created but not been attached to an element yet.

Otherwise it returns false.




PreviousNext

Related