This content originally appeared on DEV Community and was authored by CodeGuage
Table of contents
- Text Content
- Redefining
replaceChild()
- Node Count
- Redefining
insertAdjacentElement()
- Redefining
firstElementChild
- Building Tables
- Redefining
className
- HTML Serialization
- Dynamic Lists
-
append()
Polyfill
If you're an experienced JavaScript developer, working mainly on the frontend, you know the important of the HTML DOM API.
All the nice, juicy interactions and dynamic content changes that we appreciate all day long on webpages essentially reduce down to two APIs: HTML DOM and Events.
Obviously other APIs are useful too but these are the two big elephants in the room.
Understanding what exactly the HTML DOM is and how to work its plethora of utility methods isn't really that much challenging. Everything is quite simple and sound, given that you're learning HTML DOM from a good resource.
Anyways, supposing that you know the HTML DOM really well and are starting to feel the itch in your hands to exercise this knowledge of yours, where should you turn too?
Well, worry not, for here I am presenting you 10 exercises to help you apply your HTML DOM skills alongside important concepts in JavaScript.
Let's begin on the count of 3, 2, 1...
(Who was responsible for the drum roll? Duh. I guess I'll have to do it myself. π₯π₯)
Text Content
- π Link: JavaScript HTML DOM β Text Content Exercise
- π Difficulty level: Average
- π― Objective: Manually define the
textContent
property on theNode
interface.
The textContent
property is a really handy property when we just want to obtain the textual content inside a given element node.
But since it's defined on the Node
interface, it isn't only accessible on element nodes β we can access it on text and comment nodes as well. On text and comment nodes, textContent
merely returns back their nodeValue
.
By redefining the textContent
property on the Node
interface, you'll learn about how to remove element nodes from a given node, how to add a new node, how to work with nodeValue
, and much more.
Redefining replaceChild()
- π Link: JavaScript HTML DOM β Redefining
replaceChild()
Exercise - π Difficulty level: Easy
- π― Objective: Redefine the
replaceChild()
method using other methods of theNode
interface.
The replaceChild()
method allows us to replace a particular child node of a given element with another node.
Technically, replaceChild()
can be defined entirely using two other methods of the Node
interface and that's precisely what this exercise is all about.
Node Count
- π Link: JavaScript HTML DOM β Node Count Exercise
- π Difficulty level: Average
- π― Objective: Create a
Node
instance method to count all the nodes under a given node, including itself.
When working in such tree structures as the DOM, recursion is our friend in many instances. One instance is when we wish to know exactly how many nodes lie in the subtree starting at a given node, including the node itself.
This is what this exercise is about. You'll get to work with recursion, the childNodes
property, and see how to check for given node types.
Redefining insertAdjacentElement()
- π Link: JavaScript HTML DOM β Redefining
insertAdjacentElement()
Exercise - π Difficulty level: Average
- π― Objective: Redefine the
insertAdjacentElement()
method of theElement
interface, manually in JavaScript.
Many developers are aware of the appendChild()
and insertBefore()
methods of the Node
interface. But only a few are aware of the insertAdjacentElement()
method.
insertAdjacentElement()
is a really handy way to add an element node adjacent to another element in different ways.
In this exercise, you get to redefine insertAdjacentElement()
solely using existing methods of the Node
interface and, in this way, learn about various ideas such as how to work with an element's siblings, with the insertBefore()
method, how to throw an error when provided with the wrong kind of arguments, and much more.
Redefining firstElementChild
- π Link: JavaScript HTML DOM β Redefining
firstElementChild
Exercise - π Difficulty level: Easy
- π― Objective: Redefine the
firstElementChild
property of theElement
interface, manually in JavaScript.
The Element
interface defines a firstElementChild
property that returns back the first child node of the calling element node that is an element itself. Technically, it can be defined entirely using properties of the Node
interface and that's what this exercise is about.
You'll learn about the firstChild
property of nodes, how to check whether a given node is an element or not, and review the while
loop.
Building Tables
- π Link: JavaScript HTML DOM β Building Tables Exercise
- π Difficulty level: Easy
- π― Objective: Create two variants of a function to build an HTML table based on a given array of items.
It's not really a rare thing to build an HTML table with the help of data stored in a JavaScript array. This exercise tests your skills in doing so.
But more than that, it also tests your understanding of DOM mutation via innerHTML
solely and via mutation methods such as appendChild()
and insertBefore()
.
Redefining className
- π Link: JavaScript HTML DOM β Redefining
className
Exercise - π Difficulty level: Easy
- π― Objective: Redefine the
className
property of theElement
interface, manually in JavaScript.
When we set the className
property on a given element, we basically create a class
HTML attribute on it if the attribute doesn't exist, or update the value of the attribute.
Similarly, when we get the className
property on a given element, we get the value of its class
HTML attribute.
This exercise gets you to manually implement this property and directly interface with attributes of the given element.
HTML Serialization
- π Link: JavaScript HTML DOM β HTML Serialization Exercise
- π Difficulty level: Hard
- π― Objective: Create a function to replicate the behavior of
innerHTML
when it's retrieved.
HTML serialization is the name given to the process of converting an element node to its corresponding string representation. It might seem an easy thing to do but it requires us to consider a couple of ideas while crafting the algorithm.
In this exercise, you'll learn about the attributes
property of element nodes, recursion once again, the childNodes
property of the Node
interface, and much more.
Dynamic Lists
- π Link: JavaScript HTML DOM β Dynamic Lists Exercise
- π Difficulty level: Easy
- π― Objective: Fill up those
<ol>
and<ul>
elements with<li>
elements that have adata-list
attribute on them.
It's a common thing in certain JavaScript libraries to get the developer to write out HTML elements, with certain attributes providing information to that library so that it could populate the element with content, dynamically.
This exercise aims to test you on this idea.
append()
Polyfill
- π Link: JavaScript HTML DOM β
append()
Polyfill Exercise - π Difficulty level: Easy
- π― Objective: Create a polyfill for the
append()
method of theElement
interface.
Unlike appendChild()
, the append()
method allows us to bulk-insert nodes and strings (treated as text nodes) inside a given element after its last child.
This exercise tests your understanding of document fragments and how to use them to group nodes and then insert the nodes, all at once, into the DOM.
This content originally appeared on DEV Community and was authored by CodeGuage

CodeGuage | Sciencx (2025-08-21T15:00:00+00:00) Think You Know the DOM? Prove It With These 10 Exercises!. Retrieved from https://www.scien.cx/2025/08/21/think-you-know-the-dom-prove-it-with-these-10-exercises/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.