TGViewer
Web Development - HTML, CSS & JavaScript Web Development - HTML, CSS & JavaScript @javascript_courses ยท 55.7K subscribers
Post #3533 1.86K
๐Ÿš€ JavaScript DOM Manipulation โ€” Complete Beginner Guide ๐Ÿ‘จโ€๐Ÿ’ป

The DOM Document Object Model allows JavaScript to interact with HTML elements.

Without the DOM, JavaScript cannot:

Change webpage content, Respond to button clicks, Validate forms, Create interactive websites

This is one of the most important topics for frontend development.

๐Ÿง  1. What is the DOM?

DOM stands for Document Object Model.

It represents an HTML page as a tree of objects, allowing JavaScript to access and modify elements.

Example HTML

<!DOCTYPE html>
<html>
<body>
<h1 id="title">Hello World</h1>
</body>
</html>


JavaScript can access this element using its ID.

const heading = document.getElementById("title");
console.log(heading);


๐ŸŒณ 2. DOM Tree Structure

Every HTML page is organized like a tree.

Document

โ”‚

โ””โ”€โ”€ html

โ”‚

โ”œโ”€โ”€ head

โ”‚

โ””โ”€โ”€ body

โ”‚

โ”œโ”€โ”€ h1

โ”œโ”€โ”€ p

โ””โ”€โ”€ button

JavaScript can navigate this tree to access or modify elements.

๐Ÿ” 3. Selecting Elements

By ID

const title = document.getElementById("title");


By Class

const boxes = document.getElementsByClassName("box");


By Tag Name

const paragraphs = document.getElementsByTagName("p");


Using querySelector()

Returns the first matching element.

const button = document.querySelector(".btn");


Using querySelectorAll()

Returns all matching elements.

const buttons = document.querySelectorAll(".btn");


โœ๏ธ 4. Changing Content

Using textContent

const heading = document.getElementById("title");
heading.textContent = "Welcome";


Using innerHTML

heading.innerHTML = "<span>Hello JavaScript</span>";


Difference

textContent: Plain text

innerHTML: Supports HTML

๐ŸŽจ 5. Changing Styles

const heading = document.getElementById("title");
heading.style.color = "blue";
heading.style.fontSize = "40px";


๐Ÿท๏ธ 6. Working with CSS Classes

Add Class

element.classList.add("active");


Remove Class

element.classList.remove("active");


Toggle Class

element.classList.toggle("dark");


โž• 7. Creating New Elements

const para = document.createElement("p");
para.textContent = "This is a new paragraph.";


๐Ÿ“Œ 8. Adding Elements to the Page

document.body.appendChild(para);


โŒ 9. Removing Elements

const box = document.getElementById("box");
box.remove();


๐Ÿ” 10. Replacing Elements

const newHeading = document.createElement("h2");
newHeading.textContent = "New Heading";
heading.replaceWith(newHeading);


๐Ÿ–ฑ๏ธ 11. Event Listeners

Events make webpages interactive.

Click Event

const button = document.querySelector("button");
button.addEventListener("click", () => {
console.log("Button clicked");
});


Double Click

button.addEventListener("dblclick", () => {
console.log("Double Clicked");
});


โŒจ๏ธ 12. Keyboard Events
  • โค 3
  • ๐Ÿ‘ 2
  • ๐Ÿฅฐ 2
More from @javascript_courses
  1. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  2. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  3. Oct 4, 2026DSA Handwritten Notes
  4. Sep 28, 2026To learn web design and development from basic to advanced levels, you can follow these stโ€ฆ
  5. Sep 27, 202610 Websites Every Developer & AI Enthusiast Should Bookmark โœ… roadmap.sh โ€“ Step-by-step leโ€ฆ
  6. Sep 26, 2026๐Ÿ”น Escape Characters 2๏ธโƒฃ5๏ธโƒฃ New Line You can use \n and \t: const message1 = "Hello\nWorldโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’