JavaScript DOM Manipulation

May 29, 2025 1,903 views 1 min read
Beginner Tutorial
Tutorial Navigation

JavaScript DOM Manipulation

The DOM (Document Object Model) allows JavaScript to interact with HTML elements:

// Select elements
const button = document.getElementById("myButton");
const paragraph = document.querySelector(".text");

// Modify content
paragraph.textContent = "New content!";
paragraph.style.color = "blue";

// Add event listeners
button.addEventListener("click", function() {
    alert("Button clicked!");
});

// Create new elements
const newDiv = document.createElement("div");
newDiv.className = "highlight";
newDiv.textContent = "Dynamic content";
document.body.appendChild(newDiv);

Common DOM Methods:

  • getElementById() - Select by ID
  • querySelector() - Select by CSS selector
  • addEventListener() - Attach event handlers
  • createElement() - Create new elements
Try It Yourself

Practice what you've learned with our interactive code editor. Modify the code and see the results instantly!