DOM

Published: (April 28, 2026 at 01:15 AM EDT)
1 min read
Source: Dev.to

Source: Dev.to

What is getElementById()?

It is used to select a single HTML element using its unique id attribute.
It returns an Element object if a match is found; otherwise, it returns null.

Basic usage

## Hello
let el = document.getElementById("title");
console.log(el);

Output

## Hello

Manipulating the selected element

let el = document.getElementById("title");

// Change text content
el.textContent = "Hello, JavaScript!";
el.innerText = "Hi!";

// Insert HTML
el.innerHTML = "**How**";

// Style the element
el.style.color = "red";
el.style.backgroundColor = "yellow";

// Style the page background
document.body.style.backgroundColor = "black";

Full example with a button

## Hello

click

let el = document.getElementById("title");
let btn = document.getElementById("btn");

btn.addEventListener("click", () => {
  el.innerText = "Welcome!";
});
0 views
Back to Blog

Related posts

Read more »

DOM Interview Questions

What is DOM? The Document Object Model DOM is a programming interface that represents a web page as a tree‑like structure. Each HTML element becomes an object...