Ultimate Basic of DOM Manipulation

basic  of dom


DOM manipulation is the process of modifying the Document Object Model (DOM) structure of a web page using JavaScript. The DOM is a hierarchical tree-like structure that represents the HTML content of a web page. It provides a way for web developers to access and manipulate the HTML elements and content of a web page dynamically.

In modern web development, DOM manipulation is a crucial skill for building interactive web applications. It allows developers to create dynamic user interfaces that respond to user actions, events, and data changes. DOM manipulation can be used to modify the appearance and behavior of web page elements, add or remove content, and create new content on the fly.

Some common use cases for DOM manipulation include:

  • Updating the text or attributes of HTML elements based on user input or data changes
  • Adding or removing HTML elements dynamically in response to user interactions
  • Creating dynamic forms, menus, and other user interface components
  • Animating web page elements based on user actions or predefined events
  • Building single-page applications that dynamically update the content of a web page without requiring a full page refresh.

What is the DOM?

went to learn jawa

The Document Object Model (DOM) is a programming interface for web documents that represents the structure of an HTML document as a tree-like model. The DOM tree consists of various nodes, such as elements, attributes, and text, which can be accessed and manipulated using JavaScript. The root of the DOM tree is the “document” node, which represents the entire web page.

The DOM tree is organized hierarchically, with parent and child relationships between nodes. Each element in an HTML document is represented by an element node in the DOM, with its attributes and child elements as its descendants. Text nodes represent the content of an element, and comment nodes are used to add notes to the HTML source code that are not displayed in the web page. By accessing and manipulating the nodes in the DOM, developers can dynamically change the content and structure of a web page, allowing for dynamic and interactive user experiences.

Selecting Elements

To manipulate elements in the DOM with JavaScript, it’s necessary to first select the elements. There are several methods available for selecting elements in the DOM:

  1. getElementById(): This method selects an element with a specific ID. It returns a single element, since IDs are unique in an HTML document. Example:
javascriptCopy codeconst element = document.getElementById('my-element');
  1. getElementsByClassName(): This method selects all elements with a specific class. It returns a collection of elements, even if there is only one element with the specified class. Example:
javascriptCopy codeconst elements = document.getElementsByClassName('my-class');
  1. querySelector(): This method selects the first element that matches a specific CSS selector. It returns a single element. Example:
javascriptCopy codeconst element = document.querySelector('#my-element .my-class');
  1. querySelectorAll(): This method selects all elements that match a specific CSS selector. It returns a collection of elements. Example:
javascriptCopy codeconst elements = document.querySelectorAll('.my-class');

Once an element is selected, it can be manipulated with various properties and methods available through the DOM API.

Modifying Elements

To modify elements in the DOM with JavaScript, we can first select the element(s) using one of the methods discussed earlier. Once we have a reference to the element, we can modify its content, attributes, and styles.

To modify the content of an element, we can access its innerHTML or textContent property and assign a new value to it. For example, the following code would change the text inside a <p> element with the ID “my-paragraph”:

javascriptCopy codelet paragraph = document.getElementById("my-paragraph");
paragraph.innerHTML = "New text for my paragraph";

To modify attributes of an element, we can use the setAttribute() method. For example, the following code would change the source of an <img> element with the ID “my-image”:

arduinoCopy codelet image = document.getElementById("my-image");
image.setAttribute("src", "new-image.jpg");

To modify the styles of an element, we can access its style property and modify individual CSS properties. For example, the following code would change the background color of a <div> element with the ID “my-div”:

javascriptCopy codelet div = document.getElementById("my-div");
div.style.backgroundColor = "red";

To add or remove classes to elements, we can use the classList property and its methods. For example, the following code would add the class “active” to a <button> element with the ID “my-button”:

csharpCopy codelet button = document.getElementById("my-button");

Similarly, we can use the remove() method to remove a class from an element:

csharpCopy codebutton.classList.remove("active");

Overall, modifying elements in the DOM with JavaScript allows us to dynamically update the content and appearance of a web page, providing a more engaging user experience.

Creating and Deleting Elements

Creating and Deleting Elements with DOM Manipulation

As we saw in the previous section, modifying existing elements in the DOM with JavaScript can be a powerful tool in web development. However, sometimes we need to create new elements or remove existing ones entirely. In this section, we’ll explore how to use JavaScript to create and delete elements in the DOM.

Creating New Elements

To create a new element in the DOM, we first need to use the createElement method. This method takes a string argument representing the type of element we want to create, such as “div” or “p”. Here’s an example:

javascriptCopy codeconst newDiv = document.createElement("div");

This creates a new div element and assigns it to the newDiv variable. We can then add content to the new element by modifying its innerHTML or textContent properties:

javascriptCopy codenewDiv.innerHTML = "<p>Hello, world!</p>";

This adds a new p element with the text “Hello, world!” inside the div element. Once we’ve created and populated the new element, we can add it to the DOM using the appendChild method:

javascriptCopy codedocument.body.appendChild(newDiv);

This appends the new div element to the end of the body element.

Deleting Elements

To delete an element from the DOM, we can use the remove method. This method removes the element from the DOM entirely, including all of its child nodes. Here’s an example:

javascriptCopy codeconst elementToRemove = document.getElementById("my-element");

This selects the element with the ID “my-element” and removes it from the DOM.


DOM manipulation is a powerful technique that allows us to modify, create, and delete elements on a web page using JavaScript. By understanding how to use the various DOM methods available to us, we can create dynamic and interactive web pages that respond to user input and provide a more engaging experience.

Event Handling

Sure, here’s a possible outline for a section on event handling in DOM manipulation:

Introduction to Event Handling

  • Briefly explain what events are in the context of web development
  • Discuss why event handling is important for interactivity and user experience

Adding Event Listeners

  • Explain how to add event listeners to elements in the DOM with JavaScript
  • Provide examples of using the addEventListener() method to listen for events
  • Discuss the different types of events, such as click, submit, and hover

Event Object

  • Explain what the event object is and how it provides information about the event that occurred
  • Provide examples of accessing properties of the event object, such as target and type

Event Propagation

  • Explain what event propagation is and how it works in the DOM
  • Discuss the differences between bubbling and capturing
  • Provide examples of stopping event propagation with the stopPropagation() method

Event Delegation

  • Explain what event delegation is and how it can simplify event handling in certain situations
  • Provide examples of using event delegation with parent elements to handle events for multiple child elements


  • Recap the key points discussed in the section
  • Emphasize the importance of event handling for creating interactive and engaging web experiences.


Sure, here’s an example conclusion for the blog post:

In summary, DOM manipulation is a crucial aspect of web development that allows developers to dynamically modify the contents and structure of web pages using JavaScript. By selecting, modifying, creating, and deleting elements in the DOM, developers can create interactive and engaging web applications that respond to user input and events. With a solid understanding of the DOM and JavaScript, developers can unlock the full potential of the web and create rich, dynamic experiences for users.

If you’re interested in learning more about DOM manipulation, check out the Mozilla Developer Network’s comprehensive guide to the DOM. You can also explore popular JavaScript libraries like jQuery and React, which provide powerful tools for working with the DOM in a more streamlined and efficient way. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *