+
+```
+
+```js
+const container = document.getElementById('container');
+const todolist = document.querySelector('.todo-list');
+
+// Create a new element
+var newToDo = document.createElement('li');
+newToDo.setAttribute("class", "todo-item")
+newToDo.textContent = 'Buy fruits.';
+
+// Append the new element as the last child
+todolist.appendChild(newToDo);
+
+// Create another new element
+var title = document.createElement('h2');
+newToDo.textContent = 'My tasks';
+
+// Insert the title before the list
+container.insertBefore(title, todolist);
+
+// Create yet another new element
+var lastElement = document.createElement('div');
+lastElement.textContent = 'Last Element';
+
+// Append yet another element as the last child
+container.append(lastElement);
+
+// Create and prepend a new element
+var firstElement = document.createElement('div');
+firstElement.textContent = 'First Element';
+
+// Prepend the new element as the first child
+container.prepend(firstElement);
+```
+
+### 2.5. Removing Elements
+
+- `removeChild()` method removes a specified child node from the parent node. The removed child node is returned.
+
+- `remove()` method removes the element from the DOM.
+
+```html
+
+
Child Element
+
Another Child Element
+
+```
+
+```js
+// Get the container element
+var container = document.getElementById('container');
+
+// Get the child element to be removed
+var childElement = document.getElementById('childElement');
+
+// Remove the child element using removeChild
+container.removeChild(childElement);
+
+// Get another child element to be removed
+var anotherChildElement = document.getElementById('anotherChildElement');
+
+// Remove the element using remove()
+anotherChildElement.remove();
+
+```
+### 2.6. Modifying Styles
+
+- The `style` property allows to set or get inline styles for an element. This directly modifies the style attribute of the element in the DOM.
+
+```html
+
Hello world!
+```
+
+```js
+// Get the element
+var element = document.getElementById('myElement');
+
+// Change the background color and font size using the style property
+element.style.backgroundColor = 'blue';
+element.style.fontSize = '20px';
+```
+
+- The `classList` property provides methods to add, remove, and toggle CSS classes on an element. This is a more flexible way to manage an element's classes compared to directly setting the `class` attribute.
+
+```js
+// Get the element
+var element = document.getElementById('myElement');
+
+// Add a new class to the element
+element.classList.add('newClass');
+
+// Remove an existing class from the element
+element.classList.remove('initialClass');
+
+// Toggle a class on the element (add it if it doesn't exist, remove it if it does)
+element.classList.toggle('toggledClass');
+```
+
+### 2.7. Event Handling
+
+- The `addEventListener()` method attaches an event handler to an element. It allows multiple event listeners to be added to a single element for the same event type.
+
+```html
+
Click Me
+```
+
+```js
+// Define the event handler function
+function handleClick() {
+ alert('Button was clicked!');
+}
+
+// Get the button element
+var button = document.getElementById('myButton');
+
+// Add a click event listener
+button.addEventListener('click', handleClick);
+```
+
+- The `removeEventListener()` method removes an event handler that was added with `addEventListener()`.
+
+```js
+button.removeEventListener('click', handleClick);
+```
+
+## Conclusion
+
+Mastering DOM manipulation is crucial for creating dynamic, interactive web pages. The ability to access, modify, and interact with the DOM using JavaScript allows developers to build responsive and engaging user experiences.
+
+- Understanding the DOM: By understanding the structure and representation of a web document through the DOM, developers can effectively interact with and manipulate web pages.
+
+- Accessing Elements: Methods like getElementById(), getElementsByClassName(), getElementsByTagName(), querySelector(), and querySelectorAll() enable precise selection of elements within the DOM, facilitating targeted manipulations.
+
+- Modifying Content and Attributes: Techniques such as using innerHTML, textContent, and innerText for content modification, alongside getAttribute(), setAttribute(), and removeAttribute() for attribute management, provide powerful ways to dynamically change the document's content and properties.
+
+- Creating and Inserting Elements: Methods like createElement(), appendChild(), insertBefore(), append(), and prepend() allow developers to construct and integrate new elements into the DOM, enabling the dynamic construction of web pages.
+
+- Removing Elements: Using removeChild() and remove() methods facilitates the removal of elements from the DOM, which is essential for maintaining clean and efficient document structures.
+
+- Modifying Styles: Direct manipulation of inline styles via the style property and managing classes with classList methods (add(), remove(), toggle()) offer flexible control over the appearance and styling of elements.
+
+- Event Handling: The ability to attach and remove event listeners using addEventListener() and removeEventListener() empowers developers to create interactive elements that respond to user actions, enhancing the user experience.
+
+By leveraging these DOM manipulation techniques, developers can create rich, interactive web applications that provide a seamless and dynamic user experience. Understanding and utilizing these tools effectively is key to modern web development.
From 48c26c91094ce808f4ad60c0f76a9ddee5628985 Mon Sep 17 00:00:00 2001
From: Dharshi Balasubramaniyam
<139672976+DharshiBalasubramaniyam@users.noreply.github.com>
Date: Thu, 27 Jun 2024 14:30:54 +0530
Subject: [PATCH 2/3] Update DOM manipulation in JavaScript.md
---
blog/DOM manipulation in JavaScript.md | 60 +++++++++++++-------------
1 file changed, 30 insertions(+), 30 deletions(-)
diff --git a/blog/DOM manipulation in JavaScript.md b/blog/DOM manipulation in JavaScript.md
index ec6dc6dee..3b8d4ebe3 100644
--- a/blog/DOM manipulation in JavaScript.md
+++ b/blog/DOM manipulation in JavaScript.md
@@ -79,7 +79,7 @@ The DOM plays a central role in web development by enabling developers to create
```js
// Get the element with the ID 'myElement'
-var element = document.getElementById('myElement');
+const element = document.getElementById('myElement');
// Log the element to the console
console.log(element);
@@ -95,13 +95,13 @@ console.log(element);
```js
// Get the elements with the class name 'myClass'
-var elements = document.getElementsByClassName('myClass');
+const elements = document.getElementsByClassName('myClass');
// Log the elements to the console
console.log(elements);
// Optionally, you can iterate over the elements as well
-for (var i = 0; i < elements.length; i++) {
+for (let i = 0; i < elements.length; i++) {
console.log(elements[i])
}
```
@@ -119,10 +119,10 @@ for (var i = 0; i < elements.length; i++) {
```js
// Get all
elements in the document
-var paragraphs = document.getElementsByTagName("p");
+const paragraphs = document.getElementsByTagName("p");
// Loop through and log the text content of each
element
-for (var i = 0; i < paragraphs.length; i++) {
+for (let i = 0; i < paragraphs.length; i++) {
console.log(paragraphs[i].textContent);
}
```
@@ -131,16 +131,16 @@ for (var i = 0; i < paragraphs.length; i++) {
```js
// Select the first
element in the document
-var firstParagraph = document.querySelector("p");
+const firstParagraph = document.querySelector("p");
// Select the element with id="main-title"
-var titleElement = document.querySelector("#main-title");
+const titleElement = document.querySelector("#main-title");
// Select the first element with class="intro"
-var introParagraph = document.querySelector(".intro");
+const introParagraph = document.querySelector(".intro");
// Select the first
element inside the
-var paragraphInDiv = document.querySelector("div p");
+const paragraphInDiv = document.querySelector("div p");
```
- The `querySelectorAll` method in JavaScript allows you to select and retrieve a list (or NodeList) of all elements that match a specified CSS selector within the document or within a specific element. Unlike `querySelector`, which returns only the first matching element, `querySelectorAll` returns a NodeList containing all matching elements.
@@ -148,16 +148,16 @@ var paragraphInDiv = document.querySelector("div p");
```js
// Select all
elements in the document
-var paragraphs = document.querySelectorAll("p");
+const paragraphs = document.querySelectorAll("p");
// Log the number of
elements found
console.log("Number of
elements:", paragraphs.length);
// Select all elements with class="intro"
-var introElements = document.querySelectorAll(".intro");
+const introElements = document.querySelectorAll(".intro");
// Select all
elements inside the