DEV Community

Cover image for DOM is easy: Just make it work like an Array πŸ‘
4 1 1 2 1

DOM is easy: Just make it work like an Array πŸ‘

Modifying DOM elements without a framework can be tedious. The method names are long, and simple common tasks are split into tiny steps.

Wouldn't it be nice if the DOM was a bit more like an Array? Just push a new element, call sort or even reduce all the values into one.


As part of a larger project, which I'm writing a longer post about at the moment, I built this convenient little helper that works well enough on its own that I want to write a separate little post about it.

domLense is a module of less than 100 lines that lets you pretend a DOM element is actually an array. You can index it, push and pop values, loop it with forEach and even map, reduce, etc. it to get new values.

The way it works is simple: You tell the module how to get the interesting data from an element, and how to deposit different data back to it. Optionally, you define how new elements should be created.

const transforms = {
   get(li) { return li.innerText },
   set(li, text) { li.innerText = text },
   new() { return document.createElement("li") }
}
Enter fullscreen mode Exit fullscreen mode

In this case, our "interesting data" is just the text content of a bunch of <li> elements.

All that's left now is to call domLense on an unsuspecting list element, and we'll be able to treat it just like an array:

const listElement = document.createElement("ol")
const list = domLense(listElement, transforms)

list.push("First item on the list")
list.push("Another item on the list")
list.reverse()
console.log(list.map(text => `* ${text}`).join("\n"))
Enter fullscreen mode Exit fullscreen mode

I've prepared a small codepen demo to play around with this.

The way this works isn't really that complicated. The helper simply creates a proxy that redirects integer indices to the element's child list applying the provided transformations. For any other keys, it tries looking them up in Array.prototype.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (2)

Collapse
 
greenteaisgreat profile image
Nathan G Bornstein β€’ β€’ Edited

Yooo, that's really cool! One of my teachers once said that "all the DOM is, is just a big ass object" and it's stuck with me since. Array manipulation comes to me so much easier than object manipulation does. Even though everything in JS is literally an object, but pppbbbttttt

πŸ‘πŸ‘πŸ‘

Collapse
 
__masashi__ profile image
Masashi β€’

It's actually a pretty good addition to the projects using VanillaJS...
If I happen to work on any major project using Vanilla, I surely will use this.

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’