#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

Introduction to DOM

The DOM, or Document Object Model, is the browser's live, in-memory representation of a web page as a tree of objects. When the browser reads your HTML, it builds this tree, and JavaScript uses it to read content, change text and styles, add or remove elements, and react to user actions. Anything dynamic on a page goes through the DOM.

From HTML text to a tree of nodes

HTML is just text in a file. The DOM is what that text becomes once parsed. Each element turns into an element node, the text inside it into a text node, and the whole thing hangs from a single root, the document object.

<!-- this HTML -->
<body>
  <h1>Shopping list</h1>
  <ul id="list">
    <li>Milk</li>
  </ul>
</body>

<script>
  // ...becomes a tree you can work with
  const list = document.getElementById('list');
  console.log(list.parentElement.tagName);   // "BODY"
  console.log(list.children.length);         // 1

  const item = document.createElement('li');
  item.textContent = 'Bread';
  list.append(item);                         // page now shows two items

  document.querySelector('h1').style.color = 'teal';
</script>

Elements relate to each other like a family tree: body is the parent of h1 and ul, those two are siblings, and each li is a child of the ul. Properties like parentElement, children and nextElementSibling let you move around this tree.

The DOM is not your HTML file

A key idea: the DOM can differ from the source code you wrote. If you leave out a <tbody> in a table, the browser adds one to the DOM. If your markup is invalid, the browser repairs it. And once JavaScript runs, the DOM changes while the file stays the same.

That is why "View Source" and the developer tools' Elements panel can show different things. View Source shows the original file; the Elements panel shows the current DOM. When debugging, trust the Elements panel.

The main jobs you do with the DOM

  • Select elements with getElementById, querySelector and querySelectorAll.
  • Read and change content through textContent, value or attributes.
  • Change appearance with classList.add, classList.toggle or the style property.
  • Create and remove elements with createElement, append and remove.
  • Respond to events with addEventListener.

The script-runs-too-early problem

The browser builds the DOM from top to bottom. If a script in the <head> tries to find #list, that element does not exist yet, so getElementById returns null, and the next line throws "Cannot read properties of null". Fix it by placing the script just before </body>, adding the defer attribute to the script tag, or waiting for the DOMContentLoaded event.

A word on safety and speed

Prefer textContent over innerHTML when inserting text that came from users. innerHTML parses its string as HTML, so a malicious value could inject scripts. Also, each DOM change can make the browser recalculate layout; when adding many items, build them first (for example in a DocumentFragment) and append them in one step.

The DOM is a standard

The DOM is not part of the JavaScript language itself. It is a web standard that browsers implement and expose to JavaScript. Node.js, for example, runs JavaScript but has no document at all. The next lessons look at the different parts of the DOM and the standard methods it provides.

Ready to master real-world web development?

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

Explore Course