npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@voxpelli/dom-utils

v2.0.2

Published

Basic small typed DOM helpers that aids in the creation of vanilla JS code

Readme

DOM Utils

Basic small typed DOM helpers that aids in the creation of vanilla JS code.

Helps query, create and modify DOM-nodes. Like a mini-jQuery. Somewhat inspired by Bliss.js.

Based on @hdsydsvenskan/dom-utils which was based on my personal utility functions.

npm version npm downloads neostandard javascript style Module type: ESM Types in JS Follow @voxpelli@mastodon.social

Usage

import {
  $,
  createChild,
} from '@voxpelli/dom-utils';

const elem = $('.a-nice-selector');

createChild(elem, 'div', 'a-nice-selector__bemish-elem', 'With some nice text in it');

⚠️ Version 2.0.0 Breaking Changes & Deprecations

Many helpers are now deprecated in favor of modern DOM APIs.

Deprecated functions and their modern replacements

| Deprecated Function | Use Instead (Modern DOM API) | |----------------------|-----------------------------------------| | hasClass | element.classList.contains() | | addClass | element.classList.add() | | removeClass | element.classList.remove() | | toggleClass | element.classList.toggle() | | appendChild | element.append() | | emptyElement | element.replaceChildren() | | removeElement | element.remove() | | getDataAttribute | element.dataset | | setDataAttribute | element.dataset | | closestByClass | element.closest() | | is | element.matches() |

These deprecated helpers are still available for backward compatibility, but you are encouraged to migrate to the standard DOM APIs for better performance and future compatibility.

Other changes

  • Function signatures now use more precise types (e.g. ParentNode, HTMLElementTagNameMap).
  • setAttributes now sets attributes also to falsy non-null/undefined values.

API

Table of Contents

Querying & Traversing

$()

Like $$(), but returns only a single HTML element. A thin wrapper around querySelector.

If one needs to match against the context container element itself, then use elemByClass() instead.

Syntax
$(selector, [context]) => HTMLElement | undefined
Arguments
  • selectorstring | Node – A CSS selector string, or a node.
  • contextParentNode – Optional context from which to search. Defaults to document.
Returns

A single HTMLElement or undefined if no match is found.

$$()

Get an array of HTML elements that matches the specified selector. A thin wrapper around querySelectorAll.

Syntax
$$(selector, [context]) => HTMLElement[]
Arguments
  • selectorstring | Node[] | NodeListOf<Node> – A CSS selector string, or an array/NodeList of nodes.
  • contextParentNode – Optional context from which to search. Defaults to document.
Returns

An array of HTMLElements.

closestByClass()

[!WARNING] Deprecated: Use element.closest() instead.

Iterates over the parents of a node and returns the first one that has the specified class name.

Syntax
closestByClass(elem, className) => HTMLElement | undefined
Arguments
  • elemNode
  • classNamestring
Returns

A single HTMLElement or undefined if no match is found.

elemByClass()

Like $(), but with class name rather than selector + also matches against the context itself, not just elements within it.

Syntax
elemByClass(className, [context]) => HTMLElement | undefined
Arguments
  • classNamestring
  • contextParentNode

elemsByClass()

Like elemByClass() but returns an array of all matching elements within the context.

Syntax
elemsByClass(className, [context]) => HTMLElement[]
Arguments
  • classNamestring
  • contextParentNode
Returns

An array of HTMLElements.

is()

[!WARNING] Deprecated: Use element.matches() instead.

Checks if an element matches a given selector.

Syntax
is(elem, selector) => boolean
Arguments
  • elemElement
  • selectorstring
Returns

A boolean

Creation & Insertion

addText()

Adds text nodes to the supplied element, persisting newlines by adding <br> elements for each newline.

Syntax
addText(elem, text) => void
Arguments
  • elemElement
  • textstring

appendChild()

[!WARNING] Deprecated: Use element.append() instead.

Helper to append many children nodes at once.

Syntax
appendChild(elem, ...children) => void
Arguments
  • elemParentNode
  • children...Node

createChild()

Like createElement(), but also appends the created element to a container.

Syntax
createChild(elem, tag, [classNameOrAttributes], [text]) => HTMLElement
Arguments
  • elemParentNode
  • tagkeyof HTMLElementTagNameMap
  • classNameOrAttributesstring | string[] | { [attributeName: string]: string }
  • textstring
Returns

The created HTMLElement.

createElement()

Helper to easily create a new HTML element, with all that one would need for that.

Syntax
createElement(tag, [classNameOrAttributes], [text]) => HTMLElement
Arguments
  • tagkeyof HTMLElementTagNameMap
  • classNameOrAttributesstring | string[] | { [attributeName: string]: string }
  • textstring
Returns

The created HTMLElement.

insertBefore()

Inserts a child node before a given element.

Syntax
insertBefore(elem, child) => Node
Arguments
  • elemNode – The node to insert before.
  • childNode – The node to insert.
Returns

The inserted child node.

Attributes & Classes

addClass()

[!WARNING] Deprecated: Use element.classList.add() instead.

Syntax
addClass(elem, className) => void
Arguments
  • elemElement
  • classNamestring

getDataAttribute()

[!WARNING] Deprecated: Use element.dataset instead.

Helper that makes one don't have to do kebab case conversion oneself.

Syntax
getDataAttribute(elem, attribute) => string | undefined
Arguments
  • elemHTMLElement
  • attributestring – Should be in kebab case.

hasClass()

[!WARNING] Deprecated: Use element.classList.contains() instead.

Syntax
hasClass(elem, className) => boolean
Arguments
  • elemElement
  • classNamestring

removeClass()

[!WARNING] Deprecated: Use element.classList.remove() instead.

Syntax
removeClass(elem, className) => void
Arguments
  • elemElement
  • classNamestring

setAttributes()

Helper to easily set a collection of attributes on an element.

Syntax
setAttributes(elem, attributes) => void
Arguments
  • elemElement
  • attributes{ [attributeName: string]: string }

setDataAttribute()

[!WARNING] Deprecated: Use element.dataset instead.

Helper that makes one don't have to do kebab case conversion oneself.

Syntax
setDataAttribute(elem, attribute, value) => void
Arguments
  • elemHTMLElement
  • attributestring – Should be in kebab case.
  • valuestring

toggleClass()

[!WARNING] Deprecated: Use element.classList.toggle() instead.

Syntax
toggleClass(elem, className) => void
Arguments
  • elemElement
  • classNamestring

Manipulation & Removal

emptyElement()

[!WARNING] Deprecated: Use element.replaceChildren() instead.

Iterates over all children in a container and removes them all.

Syntax
emptyElement(elem) => void
Arguments
  • elemParentNode

removeElement()

[!WARNING] Deprecated: Use element.remove() instead.

Syntax
removeElement(elem) => void
Arguments
  • elemChildNode

Types

ElementContainer

A type alias for a DOM element that can contain other elements.

export type ElementContainer = Document | DocumentFragment | Element;