What in the world is DOM?

mayuhayakawa

Mayu Hayakawa

Posted on February 13, 2023

What in the world is DOM?

Hi! It's Mayu.
In this post, I'm going to explain about DOM.


What is DOM ?

DOM stands for Document Object Model, it is the mechanism for operating HTML elements.
In the DOM, elements contained in the HTML document and the text data contained in the elements are treated as objects. These objects are calle “nodes”, and the DOM views an HTML documents as tree of nodes.


The DOM world

Here is image of DOM stracture.

Each boxes are elements be called “nodes”
All nodes connects with other nodes as parents or children relations.
ex) HTML is parent of Head and Body, and child of Document.


What can we do with the DOM ?

The DOM allows us to get, change, remove nodes from HTML documents.
For example, I’ll show some method the way to get nodes! :)
Here is sample document.

<body>
    <header>
        <h1>Page title</h1>
    </header>
    <main>
        <h2>Main</h2>
        <div>
            <h2 id="section-name">Today's topic</h2>
            <ul>
                <li class="topic-name">Topic 1</li>
                    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
                <li>Topic 2</li>
                    <p>Aenean commodo ligula eget dolor.</p>
                <li>Topic 3</li>
                    <p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
            </ul>
        </div>
    </main>
    <footer>
        <p>this is footer</p>
    </footer>
</body>
Enter fullscreen mode Exit fullscreen mode

When you want to get any node, you have some choices depends the propaties that the node has.

Get element using element’s id

If the target node has id, you can get it with getElementById() method.

let elementGetById = document.getElementById("section-name");
Enter fullscreen mode Exit fullscreen mode

It returns an HTML element.

console.log(elementGetById);
// <h2 id="section-name">Today's topic</h2>
Enter fullscreen mode Exit fullscreen mode

Get elements using element’s class name

If the target nodes have class name, you can get these with getElementByClassName() method.
Because of more than two nodes can have the same class name, when you get element with getElementByClassName() the element is returned as array. So you need to know it is not single element.

let elementGetByClassName = document.getElementsByClassName("topic-name");
Enter fullscreen mode Exit fullscreen mode

It returns an array as HTML Collection (in thin case, its length is 3)

console.log(elementGetByClassName);
// [ {0:li.topic-name}, {1:li.topicname}, {2:li.topicname,} ]
Enter fullscreen mode Exit fullscreen mode

Get elements using element’s tag

Also you can get element with tag name by getElementByTagName() method. This method gets an array as well as getElementByClassName.

let elementGetByTagName = document.getElementsByTagName("p");
Enter fullscreen mode Exit fullscreen mode

It returns an array as HTML Collection (in thin case, its length is 4, 3 are children of li tag and 1 is footer’s.)

console.log(elementGetByTagName);
// [ {0:p}, {1:p}, {2:p}, {3:p} ]
Enter fullscreen mode Exit fullscreen mode

Get elements using CSS selector

When you want to get element with CSS selector, you can use querySelectore() method.
This method returns the first element that matches a specific CSS selectore. And you can get element with many ways.

with id

let elementIdWithClass = document.querySelector("#section-name");
Enter fullscreen mode Exit fullscreen mode

It returns an HTML element.

console.log(elementIdWithClass);
// <h2 id="section-name">Today's topic</h2>
Enter fullscreen mode Exit fullscreen mode

with class name

let elementClassNameWithClass = document.querySelector(".topic-name");
Enter fullscreen mode Exit fullscreen mode

It returns an HTML element.

console.log(elementClassNameWithClass);
// <li class="topic-name">Topic 1</li>
Enter fullscreen mode Exit fullscreen mode

with tag name

let elementTagNameWithClass = document.querySelector("p");
Enter fullscreen mode Exit fullscreen mode

It returns an HTML element.

console.log(elementTagNameWithClass);
// <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
Enter fullscreen mode Exit fullscreen mode

with more specific CSS selector

let elementWithSpecificClass = document.querySelector("footer p");
Enter fullscreen mode Exit fullscreen mode

It returns an HTML element.

console.log(elementWithSpecificClass);
// <p>this is footer</p>
Enter fullscreen mode Exit fullscreen mode

Get more than 2 elements using CSS selector

When you want to get all elements that matches a specific CSS selector, querySelectorAll() method allows it. And you need to know it returns a NodeList, not single HTML element, not an array.

let elementAllWithClass = document.querySelectorAll("p");
Enter fullscreen mode Exit fullscreen mode

It returns a NodeList.

console.log(elementAllWithClass);
// [ {0:p}, {1:p}, {2:p}, {3:p} ]
Enter fullscreen mode Exit fullscreen mode

What is difference between HTMLCollection and NodeList ?

As I mentioned earlier, only querySelectorAll() returns NodeList. The differences between the two are...

Dynamic or Static

When number of elements of HTMLCollection or NodeList changes,
HTMLCollection is dynamic then it can catch the change. But NodeList is static then its number of elements doesn't change.

Availability of forEach()

Available methods are different between HTMLCollection and NodeList. And common one is forEach method. HTMLCollection is available forEach(), but NodeList is not. So when you use loop process with NodeList, you should other method.

💖 💪 🙅 🚩
mayuhayakawa
Mayu Hayakawa

Posted on February 13, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related