JavaScript DOM Manipulation: Select Elements, Change Content, Attributes, Styles & Create/Remove Nodes Guide

1. What is the DOM in JavaScript?

Q: What is the DOM in JavaScript?

The Document Object Model (DOM) is a programming interface that represents an HTML document as a tree of objects. JavaScript uses the DOM to interact with and manipulate a webpage's structure, content, and styles dynamically.

2. What is DOM manipulation?

Q: What is DOM manipulation?

DOM manipulation is the process of using JavaScript to modify a webpage by adding, removing, or updating elements, attributes, or content in the DOM tree.

3. Why is DOM manipulation important in web development?

Q: Why is DOM manipulation important in web development?

DOM manipulation enables dynamic and interactive web experiences, such as updating content without reloading the page, handling user inputs, and creating responsive interfaces.

4. How does JavaScript interact with the DOM?

Q: How does JavaScript interact with the DOM?

JavaScript interacts with the DOM through the document object, which provides methods and properties to select, modify, and manipulate elements (e.g., document.getElementById).

5. How can you select elements in the DOM using JavaScript?

Q: How can you select elements in the DOM using JavaScript?

Common methods to select DOM elements include:

6. What is the difference between getElementById and querySelector?

Q: What is the difference between getElementById and querySelector?

7. What is the difference between HTMLCollection and NodeList?

Q: What is the difference between HTMLCollection and NodeList?

8. Can you give an example of selecting DOM elements?

Q: Can you give an example of selecting DOM elements?

<!DOCTYPE html>
<html>
<body>
  <h1 id="title">Hello</h1>
  <p class="text">Welcome</p>
  <p class="text">to Codeverse</p>

  <script>
    // Select by ID
    let title = document.getElementById("title");
    console.log(title); // <h1 id="title">Hello</h1>

    // Select by class (HTMLCollection)
    let texts = document.getElementsByClassName("text");
    console.log(texts[0]); // <p class="text">Welcome</p>

    // Select by tag (HTMLCollection)
    let paragraphs = document.getElementsByTagName("p");
    console.log(paragraphs); // HTMLCollection of <p> elements

    // Select with querySelector
    let firstText = document.querySelector(".text");
    console.log(firstText); // <p class="text">Welcome</p>

    // Select all with querySelectorAll
    let allTexts = document.querySelectorAll(".text");
    console.log(allTexts); // NodeList of <p class="text"> elements
  </script>
</body>
</html>
      

9. How do you select multiple elements at once?

Q: How do you select multiple elements at once?

Use document.getElementsByClassName, document.getElementsByTagName, or document.querySelectorAll to select multiple elements. querySelectorAll is preferred for its flexibility with CSS selectors.

10. What happens if you try to select an element that doesn’t exist?

Q: What happens if you try to select an element that doesn’t exist?

11. How can you change the content of a DOM element?

Q: How can you change the content of a DOM element?

Modify an element’s content using properties like:

12. What is the difference between innerHTML and textContent?

Q: What is the difference between innerHTML and textContent?

13. How do you change an element’s attributes?

Q: How do you change an element’s attributes?

Use methods like:

14. How do you modify an element’s style?

Q: How do you modify an element’s style?

Use the style property (e.g., element.style.color = "blue";) or setAttribute("style", "color: blue;"). Use camelCase for CSS properties in style (e.g., backgroundColor instead of background-color).

15. Can you give an example of changing content and attributes?

Q: Can you give an example of changing content and attributes?

<!DOCTYPE html>
<html>
<body>
  <h1 id="title">Hello</h1>
  <img id="myImage" src="old.jpg" alt="Old Image">

  <script>
    // Select elements
    let title = document.getElementById("title");
    let image = document.getElementById("myImage");

    // Change content
    title.textContent = "New Title"; // Changes text to "New Title"
    title.innerHTML = "<b>New Title</b>"; // Renders as bold

    // Change attributes
    image.setAttribute("src", "new.jpg"); // Changes image source
    image.alt = "New Image"; // Changes alt text using dot notation

    // Change style
    title.style.color = "blue";
    title.style.fontSize = "24px";

    // Remove attribute
    image.removeAttribute("alt");

    console.log(title); // <h1 id="title" style="color: blue; font-size: 24px;"><b>New Title</b></h1>
    console.log(image); // <img id="myImage" src="new.jpg">
  </script>
</body>
</html>
      

16. How do you add a new element to the DOM?

Q: How do you add a new element to the DOM?

Create an element with document.createElement, set its properties/content, and append it using methods like appendChild or append:

let div = document.createElement("div");
div.textContent = "New Div";
document.body.appendChild(div);
      

17. How do you remove an element from the DOM?

Q: How do you remove an element from the DOM?

Use the remove method or parentNode.removeChild:

let element = document.getElementById("title");
element.remove(); // Removes the element
// Or: element.parentNode.removeChild(element);
      

18. What is the difference between append and appendChild?

Q: What is the difference between append and appendChild?

19. Can you toggle a class on an element?

Q: Can you toggle a class on an element?

Yes, use the classList property:

20. What are some common mistakes to avoid in DOM manipulation?

Q: What are some common mistakes to avoid in DOM manipulation?