| | |

Salesforce LWC Master Class: What is the DOM? (Ep. 2)

In Episode 2 of the Lightning Web Component Master Class, we’ll tackle a concept that sounds intimidating but is the absolute bedrock of frontend development: The DOM (Document Object Model).

If you are a Salesforce developer moving from Apex/Visualforce to LWC, understanding the DOM is crucial because it bridges the gap between the code you write (HTML) and what the user actually interacts with in the browser. Here is a breakdown of the key concepts and the “Salesforce Analogy” that makes it all click.

What is the DOM? (It’s Not Just HTML)

A common misconception is that the DOM is simply the HTML source code of a page. While they look similar, there is a distinct difference.

  • HTML: Think of this as the blueprint or the raw data file (like a CSV).
  • DOM: This is the live, in-memory object representation created by the browser’s rendering engine based on that HTML.

The Salesforce Analogy

To explain this to Salesforce professionals, Matt uses a brilliant analogy:

HTML is to the DOM as a CSV file is to Salesforce Records.

  1. You have a CSV file full of data (HTML).
  2. You use Data Loader to push that into Salesforce.
  3. Salesforce creates Records (The DOM) based on that data.

Just as you use SOQL to query Salesforce records, in web development, you use JavaScript to query and manipulate the DOM elements.

The DOM Tree

The DOM is structured as a hierarchical tree, often referred to as the DOM Tree. It defines Parent-Child relationships between elements.

  • The <html> tag is the ultimate parent.
  • The <body> is a child of <html>.
  • A <div> might be a child of <body>, and a <p> might be a child of that <div>.

This hierarchy is essential because when we eventually write JavaScript, we often need to “traverse the tree” (e.g., “find the parent of this button” or “find all children of this div”).

Shadow DOM vs. Light DOM

In the LWC world, you will hear about different “types” of DOMs. Here is the cheat sheet:

1. Shadow DOM (The LWC Standard)

By default, all Lightning Web Components render in the Shadow DOM.

  • Purpose: Encapsulation.
  • Benefit: It acts as a protective barrier. Styles and scripts defined inside your component cannot accidentally “bleed out” and break other parts of the page, and external styles cannot easily break your component. It protects you from yourself and your teammates.

2. Light DOM

“Light DOM” is simply a fancy term for the Regular DOM.

  • The term was coined to differentiate the standard DOM from the Shadow DOM.
  • You can configure LWCs to render in Light DOM (usually to work with third-party libraries), but you lose that protective encapsulation.

3. Synthetic Shadow DOM

This is a “fake” Shadow DOM used for backwards compatibility. Older browsers that don’t natively support Shadow DOM use this polyfill to mimic the behavior.

Hands-On: Visualizing the Tree

In the video, we update the demo_lwc component from Episode 1 to visualize these relationships.

The HTML Update:

We structure the component with nested elements:

HTML

<template>
    <div>
        <h3>Hey bro what up</h3>
        <p>Hello World</p>
    </div>
    <div>
        <button>Button</button>
    </div>
</template>

The Inspection:

After deploying this to a new Lightning App Page, we can use the browser’s “Inspect” tool.

  1. The browser renders a top-level element for the component (e.g., c-demo-lwc).
  2. Inside, you see the div elements as children.
  3. Inside the first div, you see the h3 and p tags as grandchildren.

This visual representation in the browser’s Elements tab is the DOM Tree.

Summary

Understanding the DOM is the prerequisite for the fun part: interactivity. You define the structure with HTML, the browser builds the DOM, and soon, we will use JavaScript to manipulate that DOM to make buttons click, text change, and data load.

Up Next: Now that we know what we are building, the next episodes will dive deeper into HTML Basics and CSS to make our components look professional.


Resources Mentioned:

Check out the full video here: Salesforce LWC Master Class (Ep.2) – What is the DOM?


Need help with your Salesforce Org?

If you need help with your Salesforce org, schedule an hour of consulting time with me! I’m one of only ~500 Salesforce Certified Technical Architect’s (CTA) worldwide and I’ve spent over 30,000 hours building Salesforce implementations over the last decade!

Schedule and hour of consulting with me here!


Do you want to be the next Salesforce Certified Technical Architect (CTA)?

If you need training to help you on your journey to complete your Salesforce CTA Board then why not sign up for the cheapest Salesforce CTA course out there, with someone who has training over a million Salesforce professionals worldwide! You can check out and enroll in the course below!

Sign up for the CTA course here!

Or if a course isn’t your thing, you can always sign up for an hour long CTA study session with me here:
Schedule 1-on-1 CTA Coaching Here!


Get Coding With The Force Merch!!

We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.

Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!


Check Out More Coding With The Force Stuff!

If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!

Youtube
Patreon
Github
Facebook
Twitter
Instagram


Salesforce Development Books I Recommend

Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps

Good Non-SF Specific Development Books:

Clean Code
Clean Architecture


AI DISCLAIMER: Gemini assisted me in writing this blog post by analyzing and summarizing the contents on the YouTube video I created that is linked at the top of this post.

Similar Posts