Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptDifferent ways to access HTML elements using JavaScript

Different ways to access HTML elements using JavaScript

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.

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 the 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 in the Chrome web browser by pressing ctrl + shift + I.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
 
<body>
    <!-- Heading element with neveropen id-->
    <h1 id="Geeksforneveropen">
        neveropen
    </h1>
    <p>DOM getElementById() Method</p>
    <script>
        // Accessing the element by getElementById method
        let temp = document.getElementById("Geeksforneveropen");
 
        console.log(temp);
        console.log(temp.innerHTML);
    </script>
</body>
</html>


Output:

Get HTML element by className: In javascript, the getElementsByClassName() method is useful to access the HTML elements using the className. The developers can use a 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 in the Chrome web browser by pressing ctrl + shift + I.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
 
<body>
    <!-- Multiple html element with neveropen class name -->
    <h1 class="neveropen">neveropen sample 1</h1>
    <h1 class="neveropen">neveropen sample 2</h1>
    <h1 class="neveropen">neveropen sample 3</h1>
 
    <p>DOM getElementsByclassName() Method</p>
    <script>
 
        // Accessing the element by getElementsByclassName method
        let temp = document.getElementsByClassName("neveropen");
        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 in the Chrome web browser by pressing ctrl + shift + I.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
 
<body>
    <!-- Multiple html element with neveropen class name -->
    <h1 class="neveropen neveropen">neveropen sample 1</h1>
    <h1 class="neveropen">neveropen sample 2</h1>
    <h1 class="neveropen">neveropen sample 3</h1>
 
    <p>DOM getElementsByclassName() Method</p>
    <script>
 
        // Accessing the element by getElementsByclassName
        // method with multiple class
        let temp = document.getElementsByClassName(
            "neveropen neveropen");
 
        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 in the Chrome web browser by pressing ctrl + shift + I.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
 
<body>
    <!-- Multiple html element with neveropen name -->
    <h1 name="neveropen">neveropen sample 1</h1>
    <h1 name="neveropen">neveropen sample 2</h1>
    <h1 name="neveropen">neveropen sample 3</h1>
 
    <p>DOM getElementsByName() Method</p>
    <script>
 
        // Accessing the element by getElementsByName method
        let temp = document.getElementsByName("neveropen");
        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 in the Chrome web browser by pressing ctrl + shift + I.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
 
<body>
    <!-- Multiple html element with h1 tag -->
    <h1>neveropen sample 1</h1>
    <h1>neveropen sample 2</h1>
    <h1>neveropen sample 3</h1>
 
    <p>DOM getElementsByTagName() Method</p>
 
    <script>
        // Accessing the element by
        // getElementsByTagName method
        let 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 lang="en">
<head>
    <title>Document</title>
</head>
 
<body>
    <!-- html element with classnames and id -->
    <h1 class="gfg1" id="g1">neveropen sample 1</h1>
    <h1 class="gfg1" id="g2">neveropen sample 2</h1>
    <p class="gfg1">neveropen sample 3</p>
 
    <script>
        // Accessing the element by class name
        // using querySelector
        let 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 lang="en">
<head>
    <title>Document</title>
</head>
 
<body>
    <!-- html element with classnames and id -->
    <h1 class="gfg1" id="g1">neveropen sample 1</h1>
    <h1 class="gfg1" id="g2">neveropen sample 2</h1>
    <p class="gfg1">neveropen sample 3</p>
 
    <p class="gfg1">neveropen sample 4</p>
 
 
    <script>
 
        // Accessing the element by class name, id and
        // tag name using querySelectorAll
        let 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:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments