Open In App

How to Manipulate DOM Elements in JavaScript ?

In web development, one of the main features to enable interactivity is DOM Manipulation. DOM manipulation allows developers to interact and modify the structure, style, and content of web pages dynamically.

The below list contains different methods to manipulate DOM.

DOM Element Selectors

There are multiple methods available in DOM to select the HTML elements and manipulate them as listed below:

Example: The below code explain the use of the above listed DOM selectors.

<!DOCTYPE html>
<html>

<head>
    <title>
        GeeksForGeeks
    </title>
</head>

<body style="text-align: center;">
    <div id="target">
        <h1>Hello Geeks</h1>
        <h2 class="example">
            This is a sample sub heading
        </h2>
        <p>
            This is sample paragraph1
        </p>
        <p>
            This is sample paragraph2
        </p>
        <input type="text" name="username" 
            id="userName">
    </div>
    <script>
        const queryS = 
              document.querySelector(".example");
        const gebi = 
              document.getElementById("target");
        const gebcn = 
              document.getElementsByClassName('example');
        const gebtn = 
              document.getElementsByTagName('p');
        const gebn = 
              document.getElementsByName('username');
        const querySAll = 
              document.querySelectorAll("p");
        console.log(queryS);
        console.log(gebi);
        console.log(gebcn);
        console.log(gebn);
        console.log(gebtn);
        console.log(querySAll);
    </script>
</body>

</html>

Output:

selectorOP

DOM content manipulators

The below DOM properties can be used to change the HTML and the text content of the HTML elements.

Example: The below code will explain the use of the above listed DOM manipulators.

<!DOCTYPE html>
<html>

<head>
    <title>
        DOM Manipulation Techniques
    </title>
</head>

<body style="text-align: center;">
    <h1>Hello</h1>
    <h2>Hello</h2>
    <h3>Hello</h3>
    <script>
        const tcontentexample = 
            document.querySelector("h1");
        const innerhtmlexample = 
            document.querySelector("h2");
        const innertextexample = 
            document.querySelector("h3");
        tcontentexample.textContent = 
            "Hello Geeks!- textContent";
        innerhtmlexample.innerHTML = 
            "Hello Geeks!- innerHTML";
        innertextexample.innerText = 
            "Hello Geeks! - innerText";
    </script>
</body>

</html>

Output:

DOMOP

DOM element creators

You can also create dynamic HTML elements using the below DOM methods.

Example: The below code will explain the use of the above listed DOM manipulators.

<!DOCTYPE html>
<html>

<head>
    <title>
        DOM Manipulation Techniques
    </title>
</head>

<body style="text-align: center;">
    <h1>Hello 1</h1>
    <h2>Hello 2</h2>
    <h3>Hello 3</h3>
    <script>
        const para = 
            document.createElement("p");
        para.textContent = 
            "This paragraph is created using createElement!";

        const para1 = 
            document.createTextNode("p");
        para1.textContent = 
            "This paragraph is created using createTextNode";

        document.body.appendChild(para);
        document.body.appendChild(para1);
    </script>
</body>

</html>

Output:

DOMOP

DOM CSS manipulators

The below DOM properties can be used to manipulate the CSS of the HTML elements.

Example: The below code explain the use of the above listed DOM Manipulation techniques.

<!DOCTYPE html>
<html>

<head>
    <title>
        DOM Manipulation Techniques
    </title>
    <style>
        .classList {
            color: white;
            background-color: black;
            text-align: center
        }
    </style>
</head>

<body>
    <h1>Hello 1</h1>
    <h2>Hello 2</h2>
    <h3>Hello 3</h3>
    <script>
        const tcontentexample = 
            document.querySelector("h1");
        const innerhtmlexample = 
            document.querySelector("h2");
        const innertextexample = 
            document.querySelector("h3");
        tcontentexample.style.
            color = "white";
        tcontentexample.style.
            backgroundColor = "black";
        tcontentexample.style.
            textAlign = "center";
        innerhtmlexample.style.cssText = 
        `color: white;  
        background-color: black;  
        text-align:center`;
        innertextexample.classList.add("classList");
    </script>
</body>

</html>

Output:

DOMOP

Article Tags :