Different ways to access HTML elements using JavaScript - GeeksforGeeks

06 Dec, 2021
Sometimes, users need to manipulate the HTML element without changing the code of the HTML. In this scenario, users can use JavaScript to change HTML elements without overwriting them. Before we move ahead to change the HTML element using JavaScript, users should learn to access it from the DOM (Document Object Model). Here, the DOM is the structure of the web page. 
From the DOM, users can access HTML elements in five different ways in JavaScript.
Get HTML element by Id
Get HTML element by className
Get HTML element by Name
Get HTML element by tagName
Get HTML element by CSS Selector
At below, users can see the demonstration of the above methods with the sample code. 
Get HTML element by Id: Generally, most developers use unique ids in the whole HTML document. The user has to add the id to the particular HTML element before accessing the HTML element with the id. Users can use getElementById() method to access HTML element using the id. If any element doesn’t exist with the passed id into the getElementById method, it returns the null value.
Syntax:
document.getElementById(element_ID);
Parameter: It takes the id of the element which the user wants to access.
Return value: It returns the object with the particular id. If the element with the particular id doesn’t found, it returns the NULL value.
Example: This example demonstrates the use of the getElementsById method. Also, it prints the inner HTML of a returned object into the console of the browser. Users can open the console into the chrome web browser by pressing ctrl + shift + I.
HTML
<!DOCTYPE html><html>  <head>    <title>DOM getElementById() Method</title></head>  <body>      <!-- Heading element with GeeksforGeeks id-->    <h1 id="Geeksforgeeks">        GeeksforGeeks    </h1>            <p>DOM getElementById() Method</p>            <script>          // Accessing the element by getElementById method        var temp = document.getElementById("Geeksforgeeks");                  console.log(temp);        console.log(temp.innerHTML);    </script></body>  </html>
Output:
Get HTML element by className: In javascript, getElementsByClassName() method is useful to access the HTML elements using the className. The developers can use single className multiple times in a particular HTML document. When users try to access an element using the className, it returns the collection of all objects that include a particular class.
Syntax:
document.getElementsByClassName(element_classnames);
Parameter: It takes the multiple class names of the element which the user wants to access.
Return value: It returns the collection of objects that have a particular class name. Users can get every element from the collection object using the index that starts from 0.
Example 1: This example demonstrates the use of the getElementsByClassName() method. It prints every element of the returned collection object into the console. Users can open the console into the chrome web browser by pressing ctrl + shift + I.
HTML
<!DOCTYPE html><html>  <head>    <title>DOM getElementsByClassName() Method</title></head>  <body>      <!-- Multiple html element with GeeksforGeeks class name -->    <h1 class="GeeksforGeeks">GeeksforGeeks sample 1</h1>    <h1 class="GeeksforGeeks">GeeksforGeeks sample 2</h1>    <h1 class="GeeksforGeeks">GeeksforGeeks sample 3</h1>        <p>DOM getElementsByclassName() Method</p>            <script>              // Accessing the element by getElementsByclassName method        var temp = document.getElementsByClassName("GeeksforGeeks");        console.log(temp[0]);        console.log(temp[1]);        console.log(temp[2]);    </script></body>  </html>
Output:
Example 2: If a particular element contains more than one class, users can access it by passing space-separated names of the classes as a parameter of the method. Users can open the console into the chrome web browser by pressing ctrl + shift + I.
HTML
<!DOCTYPE html><html>  <head>    <title>DOM getElementByClassName() Method</title></head>  <body>      <!-- Multiple html element with GeeksforGeeks class name -->    <h1 class="GeeksforGeeks geeks">GeeksforGeeks sample 1</h1>    <h1 class="GeeksforGeeks">GeeksforGeeks sample 2</h1>    <h1 class="GeeksforGeeks">GeeksforGeeks sample 3</h1>        <p>DOM getElementsByclassName() Method</p>            <script>              // Accessing the element by getElementsByclassName        // method with multiple class        var temp = document.getElementsByClassName(                "GeeksforGeeks geeks");                          console.log(temp[0]);    </script></body>  </html>
Output:
Get HTML element by Name: In javascript, getElementsByName() method is useful to access the HTML elements using the name. Here, the name suggests the name attribute of the HTML element. This method returns the collection of HTML elements that includes the particular name. Users can get the length of the collection using the build-in length method.
Syntax:
document.getElementsByName(element_name);
Parameter: It takes the name of the element which the user wants to access.
Return value: It returns the collection of elements that have a particular name.
Example:  This example demonstrates the use of the getElementsByName method. It prints every element with a particular name into the console. Users can open the console into the chrome web browser by pressing ctrl + shift + I.
HTML
<!DOCTYPE html><html>  <head>    <title>DOM getElementByName() Method</title></head>  <body>      <!-- Multiple html element with GeeksforGeeks name -->    <h1 name="GeeksforGeeks">GeeksforGeeks sample 1</h1>    <h1 name="GeeksforGeeks">GeeksforGeeks sample 2</h1>    <h1 name="GeeksforGeeks">GeeksforGeeks sample 3</h1>        <p>DOM getElementsByName() Method</p>            <script>              // Accessing the element by getElementsByName method        var temp = document.getElementsByName("GeeksforGeeks");        console.log(temp[0]);        console.log(temp[1]);        console.log(temp[2]);    </script></body>  </html>
Output:
Get HTML elements by TagName: In javascript, getElementsByTagName() method is useful to access the HTML elements using the tag name. This method is the same as the getElementsByName method. Here, we are accessing the elements using the tag name instead of using the name of the element.
Syntax:
document.getElementsByTagName(Tag_name);
Parameter: It takes a single parameter which is the tag name.
Return value: It returns the collection of elements that includes the tag which passed as a parameter.
Example: This example demonstrates the use of the getElementsByTagName method. It prints every element with a particular tag into the console. Users can open the console into the chrome web browser by pressing ctrl + shift + I.
HTML
<!DOCTYPE html><html>  <head>    <title>DOM getElementByTagName() Method</title></head>  <body>      <!-- Multiple html element with h1 tag -->    <h1>GeeksforGeeks sample 1</h1>    <h1>GeeksforGeeks sample 2</h1>    <h1>GeeksforGeeks sample 3</h1>        <p>DOM getElementsByTagName() Method</p>            <script>              // Accessing the element by         // getElementsByTagName method        var temp = document.getElementsByTagName("h1");        console.log(temp[0]);        console.log(temp[1]);        console.log(temp[2]);    </script></body>  </html>
Output:
Get HTML elements by CSS Selector: Users can select the HTML elements using the different CSS selectors such as class, id, and tag name at a single time. HTML elements can be retrieved using CSS selectors in two ways. The querySelector() method returns the first element that matches the particular CSS selector. The querySelectorAll() method returns all element that matches the particular CSS selector. 
To use id/class as a parameter users have to add the ‘#‘/’.‘ sign before it. Users can pass directly the tag name into the above 2 methods. Users don’t need to separate CSS selectors when passing multiple CSS selectors as parameters.
Syntax:
document.querySelector(selectors);
document.querySelectorAll(selectors);
parameter: As a parameter, it accepts different CSS selectors such as class, tag name, and id.
Return value: The querySelector() method returns the first object that matches the CSS selectors, while the querySelectorAll() method returns a collection of all objects that match the CSS selectors.
Example 1: This example demonstrates the use of the querySelector method. In the below, code we have used the different CSS selectors to access the HTML elements from the DOM.
HTML
<!DOCTYPE html><html>  <head>    <title>DOM querySelector() Method</title></head>  <body>      <!-- html element with classnames and id -->    <h1 class="gfg1" id="g1">GeeksforGeeks sample 1</h1>    <h1 class="gfg1" id="g2">GeeksforGeeks sample 2</h1>    <p class="gfg1">GeeksforGeeks sample 3</p>        <script>          // Accessing the element by class name         // using querySelector        var temp = document.querySelector(".gfg1");        console.log(temp);          // Accessing the element by id using querySelector        temp = document.querySelector("#g2");        console.log(temp);          // Accessing the element by class name and        // id using querySelector        temp = document.querySelector(".gfg1#g2");        console.log(temp);          // Accessing the element by tag name that        // includes the particular class        temp = document.querySelector("p.gfg1");        console.log(temp);    </script></body>  </html>
Output:
Example 2: This example demonstrates the use of the querySelectorAll method. The querySelectorAll() method returns the node list of all objects that match with the CSS selectors. Users can access all elements of the CSS node list using the index that starts from 0. 
HTML
<!DOCTYPE html><html>  <head>    <title>DOM querySelectorAll() Method</title></head>  <body>      <!-- html element with classnames and id -->    <h1 class="gfg1" id="g1">GeeksforGeeks sample 1</h1>    <h1 class="gfg1" id="g2">GeeksforGeeks sample 2</h1>    <p class="gfg1">GeeksforGeeks sample 3</p>      <p class="gfg1">GeeksforGeeks sample 4</p>        <script>              // Accessing the element by class name, id and        // tag name using querySelectorAll        var temp = document.querySelectorAll("h1.gfg1#g2");        console.log(temp[0]);          // Accessing the element by tag name using         // querySelectorAll        temp = document.querySelectorAll("p");        console.log(temp[0]);        console.log(temp[1]);    </script></body>  </html>
Output:
JavaScript-Questions
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
How to get character array from string in JavaScript?
How to get selected value in dropdown list using JavaScript ?
How to remove duplicate elements from JavaScript Array ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?