Advance JavaScript (DOM, BOM, Web API) / Document Object Model

Selecting Elements

πŸ“Œ What is DOM Selection?

DOM selection means finding HTML elements using JavaScript so that we can read, modify, or manipulate them (change text, style, attributes, etc.).


πŸ” 1. getElementById()

βœ”οΈ Purpose: Selects one element using its unique id.

document.getElementById("id");

πŸ§ͺ Example

<p id="message">Hello World</p>
const text = document.getElementById("message");
console.log(text);
<p id="message">Hello World</p>

πŸ“ Explanation

  • IDs are unique

  • Returns a single element

  • Fastest and most commonly used

πŸ” 2. getElementsByClassName()

βœ”οΈ Purpose: Selects multiple elements having the same class name.

document.getElementsByClassName("className");

πŸ§ͺ Example

<p class="item">Apple</p>
<p class="item">Banana</p>
const items = document.getElementsByClassName("item");
console.log(items);
HTMLCollection(2) [p.item, p.item]

πŸ“ Explanation

  • Returns HTMLCollection

  • You must access elements using index

  • Not an array

console.log(items[0].textContent);
Apple

πŸ” 3. getElementsByTagName()

βœ”οΈ Purpose: Selects elements by their HTML tag name.

document.getElementsByTagName("tagName");

πŸ§ͺ Example

<h1>Title</h1>
<h1>Subtitle</h1>
const headings = document.getElementsByTagName("h1");
console.log(headings.length);
2

πŸ“ Explanation

  • Returns HTMLCollection

  • Useful when selecting all same tag elements

πŸ” 4. querySelector()

βœ”οΈ Purpose: Selects the first matching element using CSS selectors.

document.querySelector("selector");

πŸ§ͺ Example

<p class="info">First</p>
<p class="info">Second</p>
const para = document.querySelector(".info");
console.log(para.textContent);
First

πŸ“ Explanation

  • Uses CSS selectors

  • Returns only the first match

  • Very flexible and modern

πŸ” 5. querySelectorAll()

βœ”οΈ Purpose: Selects all matching elements using CSS selectors.

document.querySelectorAll("selector");

πŸ§ͺ Example

<li>One</li>
<li>Two</li>
<li>Three</li>
const listItems = document.querySelectorAll("li");
console.log(listItems);
NodeList(3) [li, li, li]

πŸ“ Explanation

  • Returns a NodeList

  • Can use forEach()

listItems.forEach(item => {
  console.log(item.textContent);
});
One
Two
Three


πŸ” HTMLCollection vs NodeList

Feature

HTMLCollection

NodeList

Returned by

getElementsBy*

querySelectorAll

Live update

βœ… Yes

❌ No

forEach

❌ No

βœ… Yes


πŸ“Œ Common Use Cases

// Change text
document.getElementById("title").textContent = "New Title";

// Change style
document.querySelector(".box").style.background = "blue";

// Loop through multiple elements
document.querySelectorAll(".item").forEach(el => {
  el.style.color = "red";
});

🧠 Summary

  • getElementById() β†’ Single element

  • getElementsByClassName() β†’ Multiple (HTMLCollection)

  • getElementsByTagName() β†’ Multiple by tag

  • querySelector() β†’ First match

  • querySelectorAll() β†’ All matches (NodeList)