JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

DOM

javascript
junior
dom

How do you select and manipulate DOM elements in JavaScript?

dom
selectors
manipulation
elements
fundamentals
Quick Answer

Use querySelector/querySelectorAll (CSS selectors) or getElementById to select elements. Manipulate them via textContent, innerHTML, classList, style, setAttribute, and createElement/appendChild for creating new elements.

Detailed Explanation

The DOM (Document Object Model) is a tree representation of the HTML document that JavaScript can interact with.

Selecting elements:

  • document.querySelector(selector) — first match (CSS selector)
  • document.querySelectorAll(selector) — all matches (NodeList)
  • document.getElementById(id) — by ID
  • document.getElementsByClassName(class) — by class (live collection)

Modifying content:

  • element.textContent — get/set text (safe, no HTML parsing)
  • element.innerHTML — get/set HTML (avoid with user input — XSS risk)

Modifying attributes and classes:

  • element.classList.add/remove/toggle/contains
  • element.setAttribute(name, value)
  • element.style.property = value

Creating and removing:

  • document.createElement(tag)
  • parent.appendChild(child) / parent.append(child)
  • element.remove()

Code Examples

Selecting and modifying elementsJavaScript
// Selecting
const heading = document.querySelector('h1');
const buttons = document.querySelectorAll('.btn');
const main = document.getElementById('main');

// Modifying content
heading.textContent = 'New Title';

// Classes
heading.classList.add('active');
heading.classList.toggle('hidden');
heading.classList.contains('active'); // true

// Styles
heading.style.color = 'blue';
heading.style.fontSize = '24px';

// Attributes
const link = document.querySelector('a');
link.setAttribute('href', 'https://example.com');
link.getAttribute('href');

Real-World Applications

Use Cases

Form Validation UI

Displaying error messages, highlighting invalid fields, and managing form state via DOM manipulation

Theme Toggling

Adding or removing CSS classes or data attributes on the root element for dark/light mode switching

Dynamic Content Loading

Loading and inserting content into the page from APIs without triggering full page reloads

Mini Projects

DOM Todo App

beginner

Build a todo app using only vanilla DOM methods like createElement, classList, and textContent

Interactive Form Validator

intermediate

Build real-time form validation with DOM feedback including error messages and input styling

Industry Examples

jQuery

Cross-browser DOM abstraction that simplified element selection, traversal, and modification with its $() API

Web Components

Native browser APIs (Custom Elements, Shadow DOM) for creating reusable, encapsulated UI components without a framework

Resources

MDN - Document Object Model

docs

JavaScript.info - Document

article

Related Questions

What is event delegation in JavaScript and why is it useful?

junior
events
Previous
What are the most commonly used string methods in JavaScript?
Next
How does try/catch work in JavaScript and when should you use it?
PrevNext