How to get attribute from a HTML element in Javascript
Get an attribute
The following code sets and gets attributes for an element.
Example
<!DOCTYPE HTML>
<html>
<body>
<p id="textblock" class="Survey numbers">
This is a test. <!--from ww w . j a v a 2 s.c o 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>