๐ŸŒ JavaScript DOM & Browser BOM
Estimated reading: 4 minutes 43 views

๐Ÿงฉ JavaScript โ€” DOM Methods & Properties: A Complete Guide with Examples


๐Ÿงฒ Introduction โ€“ Why DOM Methods & Properties Are Crucial

If youโ€™ve ever interacted with an HTML element using JavaScriptโ€”clicked a button, updated text, or added a new elementโ€”youโ€™ve used DOM methods and properties.

The Document Object Model (DOM) enables JavaScript to dynamically manipulate HTML and CSS. Mastering its methods and properties is essential for creating responsive and interactive web applications.

By the end of this guide, you’ll be able to:

  • โœ… Access, modify, and delete DOM elements
  • โœ… Use essential DOM methods and properties
  • โœ… Traverse and manipulate the DOM tree
  • โœ… Work with attributes, classes, and element structures

๐Ÿงฑ 1. DOM Properties โ€“ Accessing Element Data

DOM properties allow you to get or set data on an element.

โœ… Common DOM Properties:

PropertyDescription
innerHTMLGet/set HTML content inside an element
textContentGet/set plain text inside an element
valueGet/set value for input, textarea, select
styleAccess inline styles (e.g., el.style.color)
classNameRead/write class attribute as a string
idGet/set the elementโ€™s ID
tagNameReturns the tag name of an element
childrenReturns a collection of child elements

๐Ÿ” Example: Accessing & Changing Properties

<p id="message">Hello World</p>
const el = document.getElementById("message");
console.log(el.textContent);       // Hello World
el.textContent = "Updated!";       // โœ… Changes text
el.style.color = "red";            // โœ… Changes text color

๐Ÿ“˜ Use textContent for plain text, and innerHTML if you need HTML formatting.


๐Ÿ”ง 2. DOM Methods โ€“ Modify the Page Dynamically

DOM methods let you create, append, remove, and find elements programmatically.

โœ… Essential DOM Methods:

MethodDescription
getElementById(id)Selects one element by ID
getElementsByClassName(class)Selects elements by class name
getElementsByTagName(tag)Selects elements by tag
querySelector(selector)Selects first match using CSS selector
querySelectorAll(selector)Selects all matches using CSS selector
createElement(tag)Creates a new element
appendChild(node)Appends a child element
removeChild(node)Removes a child element
replaceChild(new, old)Replaces one child with another
setAttribute(attr, val)Sets a custom attribute
getAttribute(attr)Gets the value of an attribute
classList.add() / remove()Modify class names dynamically

โœ๏ธ Example: Create and Insert Element

const newEl = document.createElement("div");
newEl.textContent = "I was added!";
document.body.appendChild(newEl);

โœ… Adds a new <div> with text to the end of <body>.


โœ‚๏ธ Example: Remove Element

const oldEl = document.getElementById("removeMe");
oldEl.remove(); // Modern method

โœ… Removes the element from the DOM immediately.


๐Ÿงญ 3. DOM Traversal โ€“ Navigating the DOM Tree

DOM traversal allows you to navigate between related elements.

๐Ÿ” Traversal Properties:

PropertyDescription
parentNodeGets the parent element
childNodesAll child nodes (including text)
childrenElement-only children
firstElementChildFirst child that is an element
lastElementChildLast child element
nextElementSiblingNext sibling element
previousElementSiblingPrevious sibling element

๐Ÿ” Example: Navigate and Change Parentโ€™s Style

let child = document.querySelector(".child");
child.parentNode.style.background = "yellow";

โœ… Applies style to the parent of the .child element.


๐ŸŽจ 4. Working with Classes and Attributes

๐ŸŽฏ classList API

element.classList.add("active");
element.classList.remove("hidden");
element.classList.toggle("open");
element.classList.contains("selected"); // returns true/false

โœ… Easily manage CSS classes dynamically.


๐Ÿ” Attributes

const el = document.querySelector("a");
el.setAttribute("target", "_blank");
let href = el.getAttribute("href");

โœ… Read or modify any HTML attribute from JavaScript.


๐Ÿงช Real-World Example โ€“ Create a List from Array

const items = ["HTML", "CSS", "JavaScript"];
const ul = document.createElement("ul");

items.forEach(text => {
  let li = document.createElement("li");
  li.textContent = text;
  ul.appendChild(li);
});

document.body.appendChild(ul);

โœ… Dynamically generates a list using DOM methods.


๐Ÿ’ก Best Practices

  • โœ… Use querySelector for flexible and modern element access
  • ๐Ÿง  Always check if an element exists before manipulating it
  • ๐Ÿšซ Avoid excessive use of innerHTML for security (XSS risk)
  • ๐Ÿงผ Remove unused elements to prevent memory leaks

๐Ÿ“Œ Summary

Letโ€™s recap what youโ€™ve learned:

โœ… DOM properties help you read or update content and attributes
โœ… DOM methods let you create, remove, find, and insert elements
โœ… DOM traversal allows you to navigate parent-child-sibling relationships
โœ… classList and setAttribute simplify style and data manipulation

Mastering these tools makes your JavaScript code powerful, interactive, and ready for modern web applications.


โ“ FAQs

โ“ Whatโ€™s the difference between innerHTML and textContent?

  • innerHTML: Renders HTML tags (unsafe for user input)
  • textContent: Only displays raw text (safe for user input)

โ“ What is querySelector() vs getElementById()?

  • querySelector() is more flexible and uses CSS selectors
  • getElementById() is faster and simpler for known IDs

โ“ How can I create a new HTML element using JavaScript?

Use document.createElement(tagName) and then append it:

let p = document.createElement("p");
p.textContent = "New paragraph";
document.body.appendChild(p);

โ“ What is classList used for?

classList is a DOM property that allows you to add, remove, toggle, and check CSS classes dynamically.


โ“ Is removeChild() still used?

Yes, but modern JavaScript supports element.remove() which is more concise and readable.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

JavaScript โ€” DOM Methods & Properties

Or Copy Link

CONTENTS
Scroll to Top