Skip to content
Related Articles

Related Articles

Improve Article

How getElementByID works in JavaScript ?

  • Last Updated : 21 Aug, 2021
Geek Week

The Document method getElementById() returns an element object representing the element whose id property matches with the given value. This method is used to manipulate an element on our document.

Syntax :

const gfg = document.getElementById(id);


  • id: The id of the element which we want to grab. The is a case-sensitive string that is unique within the document.

Return value:

An element object with the specified id, or null if no matching element is found. Below example illustrates the use of getElementById.



<!DOCTYPE html>
<html lang="en">
  <!-- defining an element with an id -->
  <h1 id="gfg">GeeksForGeeks</h1>
  <!-- Adding javascript -->
      // grabbing element
      const gfg = document.getElementById('gfg');

Output: Here you can see “GeeksForGeeks” printed in the console tab. Now after grabbing an element, we can perform any operations on that element like changing text, adding style etc.

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :