JavaScript DOM Manipulation (original) (raw)

Last Updated : 6 May, 2026

The Document Object Model (DOM) represents the structure of a webpage as a tree of objects. It allows JavaScript to access and modify the content, structure, and styling of HTML elements dynamically.

Manipulation of DOM Elements

We can manipulate or change the DOM elements by using the following methods:

1. Change the Content of an Element

You can change the content inside an HTML element using JavaScript. The two most common properties for this are innerHTML and textContent:

This is the original content using innerHTML.
This is the original text content using textContent.
<button onclick="changeContent()">Change Content</button>

<script>
    // Function to change content
    function changeContent() {
        document.getElementById("example1").innerHTML = 
        "<strong>This is changed using innerHTML!</strong>";

        document.getElementById("example2").textContent = 
        "This is changed using textContent!";
    }
</script>

`

2. Manipulate the Class Attribute

You can add, remove, or toggle classes on an element using JavaScript. This is helpful for styling or applying animations.

This is a text element with the "bold" class.
<button onclick="addClass()">Add 'highlight' Class</button>
<button onclick="removeClass()">Remove 'bold' Class</button>
<button onclick="toggleClass()">Toggle 'highlight' Class</button>
<script>
    function addClass() {
        document.getElementById("example").classList.add("highlight");
    }
    function removeClass() {
        document.getElementById("example").classList.remove("bold");
    }
    function toggleClass() {
        document.getElementById("example").classList.toggle("highlight");
    }
</script>

`

3. Set CSS Styles Using JavaScript

You can directly manipulate the CSS styles of an element using the style property. This allows you to dynamically change how elements appear on the page.

JavaScript `

// Changing multiple CSS properties document.getElementById("demo").style.color = "red"; document.getElementById("demo").style.fontSize = "20px"; // Adding more than one style document.getElementById("demo").style.cssText = "color: blue; font-size: 18px;";

`

4. Create, Add, and Remove Elements

Sometimes, you need to create new elements, add them to the DOM, or remove existing ones. You can do this using the following methods:

// Create a new element let newDiv = document.createElement("div"); newDiv.textContent = "This is a new div";

// Add the new element to the DOM document.body.appendChild(newDiv);

// Remove the element (modern approach) newDiv.remove();

`

5. Insert Elements at a Specific Position

You can insert new elements at specific positions relative to existing elements using methods like insertBefore().

JavaScript `

// Create a new element let newDiv = document.createElement("div"); newDiv.textContent = "This is a new div";

// Find an existing element to insert before let referenceNode = document.getElementById("referenceElement");

// Insert the new element before the reference element document.body.insertBefore(newDiv, referenceNode);

`

6. Manipulate Element Attributes

You can easily get, set, or remove the attributes of an HTML element using the following methods:

// Get the value of an attribute let src = document.getElementById("image").getAttribute("src");

// Set a new value for an attribute document.getElementById("image").setAttribute("src", "new-image.jpg");

// Remove an attribute document.getElementById("image").removeAttribute("src");

`

7. Manipulate Data Attributes

HTML5 introduced data attributes, which are custom attributes that you can use to store extra information about an element. These are particularly useful for adding data to an element without affecting its visual structure.

// Setting a data attribute document.getElementById("demo").dataset.userId = "12345"; // Getting a data attribute let userId = document.getElementById("demo").dataset.userId; console.log(userId); // Outputs: 12345

`