How to check to see the existence of an attribute in Javascript
Attribute existence
The following code uses hasAttribute
to
check the existence of an attribute.
Example
<!DOCTYPE HTML>
<html>
<body>
<p id="textblock" class="Survey numbers">
This is a test. <!--from www .j a v a 2 s. co m-->
</p>
<pre id="results"></pre>
<script>
var elem = document.getElementById("textblock");
document.writeln("lang attribute: " + elem.hasAttribute("lang"));
elem.setAttribute("lang", "en-US");
document.writeln("Attr value is : " + elem.getAttribute("lang"));
elem.setAttribute("lang", "en-UK");
document.writeln("Value is now: " + elem.getAttribute("lang"));
</script>
</body>
</html>