Home » Web Design and Development » HTML » Mastering DOM Manipulation: Customizing HTML with JavaScript

Mastering DOM Manipulation: Customizing HTML with JavaScript

The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. Understanding how to work with DOM elements using JavaScript is essential for creating dynamic and interactive web pages. In this comprehensive guide, we will explore various ways to manipulate DOM elements to customize your HTML code.

1. What is the DOM?

The DOM is a tree-like structure representing the HTML document. Each element, attribute, and text node in the HTML document is represented as a node in this tree. JavaScript can access and manipulate these nodes to update the content, structure, and style of the web page.

2. Accessing DOM Elements

To work with DOM elements, you first need to access them. There are several methods to do this:

2.1. getElementById

This method returns the element with the specified ID.

const element = document.getElementById('myElement');

2.2. getElementsByClassName

This method returns a live HTMLCollection of all elements with the specified class name.

const elements = document.getElementsByClassName('myClass');

2.3. getElementsByTagName

This method returns a live HTMLCollection of all elements with the specified tag name.

const elements = document.getElementsByTagName('div');

2.4. querySelector and querySelectorAll

These methods return the first element or all elements that match the specified CSS selector.

const element = document.querySelector('.myClass');
const elements = document.querySelectorAll('.myClass');

3. Manipulating DOM Elements

Once you have accessed the DOM elements, you can manipulate them using various properties and methods.

3.1. Changing Element Content

You can change the content of an element using the innerHTML or textContent properties.

element.innerHTML = 'New Content';
element.textContent = 'New Content';

3.2. Changing Element Attributes

You can change the attributes of an element using the setAttribute method.

element.setAttribute('src', 'newImage.png');

3.3. Changing Element Styles

You can change the styles of an element using the style property.

element.style.color = 'red';
element.style.backgroundColor = 'blue';

3.4. Adding and Removing Classes

You can add or remove classes from an element using the classList property.

element.classList.add('newClass');
element.classList.remove('oldClass');

4. Creating and Adding New Elements

You can create new elements and add them to the DOM using the createElement and appendChild methods.

4.1. Creating a New Element

Use the createElement method to create a new element.

const newElement = document.createElement('div');

4.2. Adding the New Element to the DOM

Use the appendChild method to add the new element to the DOM.

document.body.appendChild(newElement);

5. Removing Elements

You can remove elements from the DOM using the removeChild method.

5.1. Removing an Element

First, select the parent element and then remove the child element.

const parentElement = document.getElementById('parentElement');
const childElement = document.getElementById('childElement');
parentElement.removeChild(childElement);

6. Event Handling

You can add event listeners to elements to make them interactive.

6.1. Adding an Event Listener

Use the addEventListener method to add an event listener to an element.

element.addEventListener('click', function() {
    alert('Element clicked!');
});

7. Best Practices for DOM Manipulation

7.1. Minimize DOM Access

Accessing the DOM can be slow, so minimize the number of times you access it.

7.2. Use Efficient Selectors

Use efficient selectors to minimize the time it takes to find elements.

7.3. Batch DOM Updates

Batch your DOM updates to minimize reflows and repaints.

8. Some Examples

8.1. Updating the CSS / Style of element from JAVA Script

<html>
   <head>
      <script>
         window.onload = function() {
         var htmlElement = document.getElementById("h1ElementId");
         htmlElement.style = "color:red";
         htmlElement.innerHTML = "Lynxbee DOM TEST page!";
         }
         
      </script>
   </head>
   <body>
      <h1 id="h1ElementId"></h1>
   </body>
</html>

In above code, we updated the html element H1 and changed its color using “style”. This is just simple example, so you can change any style for any element.

8.2. Hide the element completely

<html>
   <head>
      <script>
         window.onload = function() {
         var htmlElement = document.getElementById("h1ElementId");
         htmlElement.style = "display:none";
         htmlElement.innerHTML = "Lynxbee DOM TEST page!";
         }
         
      </script>
   </head>
   <body>
      <h1 id="h1ElementId"></h1>
   </body>
</html>

using above code i.e. adding style “display:none” we can hide the element completely from javascript.

The code ,

htmlElement.style = "display:none";

can also be written as,

htmlElement.style.display = "none";

Working with DOM elements is a fundamental skill for any web developer. By mastering DOM manipulation using JavaScript, you can create dynamic and interactive web pages that provide a better user experience. Follow the best practices outlined in this guide to ensure your code is efficient and maintainable.


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment