Dular SharmaDular SharmaDular Sharma
(Monday - Saturday)
sharmadular7392@gmail.com
Himatnagar
Dular SharmaDular SharmaDular Sharma

Chapter 21. Modifying HTML Elements in JavaScript

39 Views

JavaScript provides powerful methods to modify HTML elements, enabling developers to create dynamic and interactive web applications. This blog will explore how to modify HTML elements using JavaScript, covering essential techniques and practical examples.

Changing Element Content

You can change the content of HTML elements using the textContent, innerHTML, and innerText properties.

Using textContent

The textContent property sets or returns the text content of the specified node.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Modifying Content with textContent</title>
</head>
<body>
    <p id="paragraph">Original Text</p>
    <button onclick="changeText()">Change Text</button>

    <script>
        function changeText() {
            document.getElementById('paragraph').textContent = 'Text has been changed!';
        }
    </script>
</body>
</html>

In this example, clicking the “Change Text” button updates the text content of the paragraph element.

Using innerHTML

The innerHTML property sets or returns the HTML content inside an element.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Modifying Content with innerHTML</title>
</head>
<body>
    <div id="content">Original Content</div>
    <button onclick="changeContent()">Change Content</button>

    <script>
        function changeContent() {
            document.getElementById('content').innerHTML = '<h2>New HTML Content</h2>';
        }
    </script>
</body>
</html>

In this example, clicking the “Change Content” button replaces the content inside the div element with new HTML content.

Using innerText

The innerText property sets or returns the visible text content of an element.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Modifying Content with innerText</title>
</head>
<body>
    <p id="paragraph">Original Text</p>
    <button onclick="changeText()">Change Text</button>

    <script>
        function changeText() {
            document.getElementById('paragraph').innerText = 'Text has been changed!';
        }
    </script>
</body>
</html>

In this example, clicking the “Change Text” button updates the visible text content of the paragraph element.

Modifying Element Attributes

You can modify the attributes of HTML elements using the setAttribute, getAttribute, and removeAttribute methods.

Using setAttribute

The setAttribute method sets the value of an attribute on the specified element.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Modifying Attributes with setAttribute</title>
</head>
<body>
    <img id="image" src="original.jpg" alt="Original Image">
    <button onclick="changeImage()">Change Image</button>

    <script>
        function changeImage() {
            document.getElementById('image').setAttribute('src', 'new.jpg');
        }
    </script>
</body>
</html>

In this example, clicking the “Change Image” button updates the src attribute of the image element.

Using getAttribute

The getAttribute method returns the value of an attribute on the specified element.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Getting Attributes with getAttribute</title>
</head>
<body>
    <img id="image" src="original.jpg" alt="Original Image">
    <button onclick="showImageSrc()">Show Image Source</button>

    <script>
        function showImageSrc() {
            let imgSrc = document.getElementById('image').getAttribute('src');
            alert('Image source: ' + imgSrc);
        }
    </script>
</body>
</html>

In this example, clicking the “Show Image Source” button displays the current src attribute of the image element in an alert.

Using removeAttribute

The removeAttribute method removes an attribute from the specified element.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Removing Attributes with removeAttribute</title>
</head>
<body>
    <img id="image" src="original.jpg" alt="Original Image">
    <button onclick="removeAlt()">Remove Alt Attribute</button>

    <script>
        function removeAlt() {
            document.getElementById('image').removeAttribute('alt');
        }
    </script>
</body>
</html>

In this example, clicking the “Remove Alt Attribute” button removes the alt attribute from the image element.

Modifying Element Styles

You can modify the styles of HTML elements using the style property.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Modifying Styles</title>
</head>
<body>
    <div id="box" style="width: 100px; height: 100px; background-color: red;"></div>
    <button onclick="changeStyle()">Change Style</button>

    <script>
        function changeStyle() {
            let box = document.getElementById('box');
            box.style.width = '200px';
            box.style.height = '200px';
            box.style.backgroundColor = 'blue';
        }
    </script>
</body>
</html>

In this example, clicking the “Change Style” button changes the dimensions and background color of the div element.

Adding and Removing Classes

You can add and remove classes from elements using the classList property.

Adding a Class

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Adding Classes</title>
    <style>
        .highlight {
            background-color: yellow;
        }
    </style>
</head>
<body>
    <p id="text">This is some text.</p>
    <button onclick="addClass()">Highlight Text</button>

    <script>
        function addClass() {
            document.getElementById('text').classList.add('highlight');
        }
    </script>
</body>
</html>

In this example, clicking the “Highlight Text” button adds the highlight class to the paragraph element, changing its background color.

Removing a Class

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Removing Classes</title>
    <style>
        .highlight {
            background-color: yellow;
        }
    </style>
</head>
<body>
    <p id="text" class="highlight">This is some highlighted text.</p>
    <button onclick="removeClass()">Remove Highlight</button>

    <script>
        function removeClass() {
            document.getElementById('text').classList.remove('highlight');
        }
    </script>
</body>
</html>

In this example, clicking the “Remove Highlight” button removes the highlight class from the paragraph element, removing its background color.

Modifying HTML elements with JavaScript allows you to create dynamic and interactive web applications. By mastering techniques such as changing content, modifying attributes, adjusting styles, and manipulating classes, you can enhance the user experience and functionality of your web projects. Experiment with these methods to improve your JavaScript skills and build more engaging web applications.

Previous Post
Newer Post

Leave A Comment

Need Help?