Basic DOM Manipulation in JavaScript - CodingQue

Basic DOM Manipulation in JavaScript

The Document Object Model (DOM) allows JavaScript to interact with the HTML structure of a web page. In this tutorial, we will explore basic techniques for selecting and manipulating elements in the DOM.

1. Selecting Elements

JavaScript provides several methods to select elements in the DOM. Understanding these methods is crucial for effective DOM manipulation:

Example of Selecting Elements

Here’s a simple example demonstrating how to select elements:

<!DOCTYPE html>
<html>
<head>
    <title>Element Selection Example</title>
</head>
<body>
    <h1 id="main-heading">Hello World</h1>
    <p class="description">This is a description paragraph.</p>
    <button id="change-text-btn">Change Text</button>

    <script>
        // Selecting elements
        const heading = document.getElementById('main-heading');
        const description = document.querySelector('.description');

        console.log(heading); // Logs the heading element
        console.log(description); // Logs the description paragraph
    </script>
</body>
</html>

Using querySelectorAll

In addition to selecting a single element, you can select multiple elements. For example, if you want to select all paragraphs:

<script>
    // Selecting all paragraph elements
    const paragraphs = document.querySelectorAll('p');
    paragraphs.forEach((para) => {
        console.log(para.textContent); // Logs the text of each paragraph
    });
</script>

2. Modifying Element Content and Style

Once you have selected an element, you can modify its content and style. This allows you to create dynamic and interactive web pages:

Example of Modifying Content and Style

In the following example, clicking the button changes the heading text and style:

<!DOCTYPE html>
<html>
<head>
    <title>Modify Content and Style</title>
    <style>
        .highlight {
            color: red;
            font-weight: bold;
            font-size: 1.5em;
        }
    </style>
</head>
<body>
    <h1 id="main-heading">Original Heading</h1>
    <button id="change-style-btn">Change Style</button>

    <script>
        const heading = document.getElementById('main-heading');
        const button = document.getElementById('change-style-btn');

        button.onclick = function() {
            heading.textContent = 'Heading Changed!';
            heading.classList.add('highlight'); // Adding a class to change style
            heading.style.backgroundColor = 'yellow'; // Changing the background color
        };
    </script>
</body>
</html>

Explanation of the Code

In this example:

3. Example: Changing Multiple Elements

Sometimes you may want to apply changes to multiple elements at once. Here’s how you can do that:

<!DOCTYPE html>
<html>
<head>
    <title>Change Multiple Elements</title>
    <style>
        .changed {
            color: green;
            font-style: italic;
        }
    </style>
</head>
<body>
    <p class="item">Item 1</p>
    <p class="item">Item 2</p>
    <p class="item">Item 3</p>
    <button id="change-items-btn">Change Items</button>

    <script>
        const items = document.querySelectorAll('.item');
        const button = document.getElementById('change-items-btn');

        button.onclick = function() {
            items.forEach(item => {
                item.classList.add('changed'); // Adding a class to change style for each item
                item.textContent = item.textContent + ' - Updated!'; // Modifying text
            });
        };
    </script>
</body>
</html>

Explanation of the Code

In this example:

Previous Next
Modern Footer