Deep dive into the Document Object Model structure and manipulation.
Example of accessing and modifying DOM elements:
// Selecting an element
const heading = document.getElementById("main-heading");
// Changing text content
heading.textContent = "Updated Heading";
// Changing style
heading.style.color = "blue";
// Creating and appending a new element
const newParagraph = document.createElement("p");
newParagraph.textContent = "This is a new paragraph.";
document.body.appendChild(newParagraph);
This code demonstrates:
getElementById
.The Document Object Model (DOM) represents the page so that programs can change the document structure, style, and content. It treats an HTML or XML document as a tree structure where each node is an object representing a part of the document.
Key DOM manipulation techniques include:
getElementById
, querySelector
, and getElementsByClassName
are used to access elements.textContent
, innerHTML
, and setAttribute
.style
property.createElement
, appendChild
, and insertBefore
.Q1: What does the DOM stand for?
Answer: C. Document Object Model
Q2: Which method is used to select an element by ID?
Answer: B. getElementById
Q3: How can you change the text of an element?
Answer: C. element.textContent
Q4: Which method is used to create a new element in the DOM?
Answer: A. createElement