HTML DOM getElementById() Method

The getElementById() method returns the element of specified ID. It returns null if the element with specified ID is not found. It is the most common DOM method, which is used to manipulate or get info from a DOM element. The syntax of getElementById() method is as follows :
  document.getElementById(elementID)
Where the elementID is ID of the element of the element we want to manipulate. Also note that an ID should be unique within a  page, if more than one element within the same ID, then the getElementById method will return the first element in the source code.

Example :
<html>
<body>
  <p id="demo">Hello world, This is a test Paragraph.</p>
  <button onclick="myfunc()">Click Here</button>
  <script>
    function myfunc() {
      var x = document.getElementById("demo");
      x.style.backgroundColor = "yellow";
      x.style.color = "blue";
      x.style.fontWeight = "bold";
      x.style.fontSize = "20px";
    }
  </script>
</body>
</html>
Output :

Hello world, This is a test Paragraph.



Next Topic :