A useful interface does not need to be complicated. Start with native HTML, then add JavaScript only when you need it.
What You Need to Know
The DOM is the browser's live tree of page nodes. An HTML element becomes a DOM element that JavaScript can find, inspect, and change while the page is open.
In this example: read the textContent of a paragraph.
Try the Example
Take a look at the example. We’ll explain the important lines right below it.
<main id="course-content">
<h1>HTML and the DOM</h1>
<p class="summary">The browser builds a tree of elements.</p>
</main>
<script>
const courseContent = document.querySelector("#course-content");
console.log(courseContent.children.length);
</script>
Let’s Read the Code
- <main>: The main element marks the primary page content.
- <h1>: A top-level heading explains what the page is about.
- id: An id uniquely identifies an element on this page.
Read the named elements and attributes together. They explain how the browser connects this feature to the rest of the page.
What Happens in the Browser?
When this example includes JavaScript, it supplies behavior that ordinary HTML does not automatically provide. Always think about a fallback.
What makes it interactive? Look for the JavaScript that finds an element and reacts to an event. HTML provides the initial controls and content; the script updates the page. Try removing the script to see which part still works natively.
Your Turn
Run this on a local development server if it loads modules or resources. Change the visible text or trigger and check what still works with scripts disabled.
Common Beginner Mistakes
- Assuming new HTML syntax works in every browser without testing.
- HTML supplies structure; JavaScript is necessary for the scripted behavior in this example. Check keyboard access and focus movement too.
Key Takeaways
- The DOM is the browser's live tree of page nodes.
- <main>: The main element marks the primary page content.
- Test the behavior you expect rather than trusting how the snippet looks in your editor.