getAttribute

In this chapter you will learn:

  1. How to get attribute from HTML element

Get an attribute

The following code sets and gets attributes for an element.

<!DOCTYPE HTML> <!-- j  a  va 2  s.c  om-->
<html> 
<body> 
<p id="textblock" class="Survey numbers"> 
    This is a test.              
</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>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. How to get element by its tag name
Home » Javascript Tutorial » HTML Operation
HTMLElement
addEventListener
appendChild
attributes
classList
className
cloneNode
createElement
createTextNode
dataset
getAttribute
getElementsByTagName
hasAttribute
innerHTML
insertAdjacentHTML
insertBefore
isSameNode
outerHTML
onmouseout
onmouseover
removeEventListener