#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Document Object Model (DOM)

The Document Object Model, or DOM, is the browser's live representation of a web page as a tree of objects. When the browser reads your HTML, it builds this tree, and JavaScript can then read and change it. Every time a script updates text, adds a list item, hides a menu or changes a class, it is working with the DOM, and the browser redraws the page to match.

A page as a tree of nodes

Each piece of the page becomes a node. The document object sits at the top, holding the html element. Inside it are head and body, and inside those every other element. Elements contain further elements and text nodes, so the structure looks like a family tree with parents, children and siblings. Even the whitespace and comments in your HTML become nodes, though you rarely work with those directly.

Node typeExampleTypical use
DocumentdocumentEntry point for finding anything
Element<p>, <button>Reading attributes, changing classes and content
TextThe words inside a paragraphHolding the actual characters
Comment<!-- note -->Rarely touched by scripts

The DOM is not your HTML file

Your HTML file is just the starting point. The DOM is what the browser built from it, and it can differ. Browsers fix mistakes as they parse, such as adding a missing tbody to a table or closing an unclosed paragraph. Once scripts run, the DOM keeps changing while the file stays the same. This is why "View source" shows the original markup, while the Elements panel in developer tools shows the current DOM.

Reading and changing it from JavaScript

const heading = document.querySelector('h1');
console.log(heading.textContent);       // read current text

heading.textContent = 'Today\'s Deals'; // change it
heading.classList.add('highlight');     // add a CSS class

const list = document.querySelector('#deal-list');
const item = document.createElement('li');
item.textContent = 'Headphones - 30% off';
list.append(item);                      // insert into the tree

console.log(list.children.length);      // number of child elements
console.log(item.parentElement === list); // true

The pattern is nearly always the same: find a node, then read or change one of its properties, or create a new node and insert it somewhere. The following lessons cover each part in detail.

Why the DOM is language-neutral

The DOM is a standard interface defined separately from JavaScript. JavaScript happens to be the language browsers use to access it, but the same model and method names appear in other environments that process HTML or XML. In the browser, the DOM methods are provided by the host, not by the JavaScript language itself, which is why they do not exist in plain Node.js without a library.

A timing problem beginners meet

Scripts can only reach elements that are already in the DOM. If a script in the head runs before the body is parsed, document.querySelector('#deal-list') returns null, and the next line throws an error. Load the script with defer, place it at the end of the body, or wait for the DOMContentLoaded event.

A second habit worth breaking early is rebuilding large sections with innerHTML on every small change. It discards and recreates nodes, losing event listeners and form input. Change only the nodes that need it.

In a sentence

The DOM is a live, changeable tree that the browser built from your HTML, and JavaScript updates the page by finding nodes in that tree and changing them.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course