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

@briantbailey/dom-once

v1.0.0

Published

Simple utilities to select and manipulate DOM elements only once.

Readme

@briantbailey/dom-once

npm version Bundle Size JSR TypeScript Types ESM Only Tree Shakeable CI License: MIT

Simple utilities to select and manipulate DOM elements only once.

Run your DOM initialization code safely, even when called multiple times. Ideal for SPAs and dynamic DOM updates. Framework-agnostic; safe after re-renders or partial updates.

// Initialize buttons only once, even if called multiple times
doOnce('btn-init', '.btn', (btn) => btn.classList.add('ready'));

Features

  • Zero dependencies — Pure DOM utilities, no external libraries
  • Enforces idempotent initialization — Prevents re-execution on already-processed elements
  • Flexible selectors — Works with CSS strings, Elements, NodeLists, or arrays
  • Customizable tracking — Use any data attribute name
  • Framework-agnostic — Pure JavaScript, works anywhere the DOM exists
  • Full TypeScript support — Complete type definitions included
  • Tree-shakeable ESM — Modern bundlers can eliminate unused code
  • Tiny footprintCheck current bundle size

How It Works

Tracks processed elements using a data attribute (default: data-dom-once) with space-delimited tokens:

<button data-dom-once="btn-init tooltip analytics">Click me</button>

Each once id marks that a specific operation has been performed, preventing duplicate initialization even when your code runs multiple times.

OnceId Rules:

  • Valid characters: letters, numbers, underscores (_), and hyphens (-)
  • Examples: btn-init, tooltip_setup, v2
  • Avoid spaces (tokens are space-delimited)
  • Case-sensitive: myId and myid are different

Installation

Package Managers

npm / pnpm / yarn:

npm install @briantbailey/dom-once

JSR (Node or Deno):

npx jsr add @briantbailey/dom-once
# or
deno add @briantbailey/dom-once

CDN

esm.sh:

Import directly as ESM in browsers or Deno:

https://esm.sh/@briantbailey/dom-once

unpkg:

Access package files directly:

https://unpkg.com/@briantbailey/dom-once@latest/dist/dom-once.js          (ESM)
https://unpkg.com/@briantbailey/dom-once@latest/dist/dom-once.iife.min.js (IIFE)

GitHub Releases:

Download the IIFE bundle directly from releases.

Usage

Node / Deno (ESM)

import { doOnce, querySelectorOnce, removeOnce, findOnce, version } from '@briantbailey/dom-once';

doOnce('btn-init', '.btn', (btn) => {
  btn.classList.add('initialized');
});

Note: This package is ESM-only. Use import in Node and bundlers; require() is not supported.

Browser (ESM via CDN)

Static import:

<script type="module">
  import { doOnce } from 'https://esm.sh/@briantbailey/dom-once';
  
  doOnce('btn-init', '.btn', (btn) => btn.classList.add('ready'));
</script>

Dynamic import:

<script>
  import('https://esm.sh/@briantbailey/dom-once')
    .then(({ doOnce }) => {
      doOnce('btn-init', '.btn', (btn) => btn.classList.add('ready'));
    });
</script>

Browser (IIFE)

The IIFE bundle exposes everything on the global domOnce object:

<script src="https://unpkg.com/@briantbailey/dom-once@latest/dist/dom-once.iife.min.js"></script>
<script>
  const { doOnce, querySelectorOnce, removeOnce, findOnce, version } = window.domOnce;
  
  doOnce('btn-init', '.btn', (btn) => btn.classList.add('ready'));
</script>

API

querySelectorOnce(onceId, selector[, options]) → Element[]

Queries for elements using a CSS selector and marks them with a once id.

  • onceId: string — Unique identifier (alphanumeric, underscore, hyphen)
  • selector: string — CSS selector string
  • options: object (optional)
    • onceAttribute: string — Data attribute name (default: 'data-dom-once')
    • context: Document | DocumentFragment | Element — Query context (default: document)
  • Returns: Element[] — Elements that matched the selector AND didn't already have the once id (i.e., newly marked). Elements already marked are excluded.

doOnce(onceId, selector, callback[, options]) → Element[]

Executes a callback once per element, marking elements to prevent re-execution.

  • onceId: string — Unique identifier
  • selector: string | Element | Iterable<Element> | ArrayLike<Element> — Elements to process
  • callback: (element: Element) => void — Function to execute on each unmarked element
  • options: object (optional)
    • onceAttribute: string — Data attribute name (default: 'data-dom-once')
    • context: Document | DocumentFragment | Element — Query context (default: document)
  • Returns: Element[] — Elements that were newly processed (matched the selector and didn't already have the once id). Elements already marked are excluded.

removeOnce(onceId, selector[, options]) → Element[]

Removes a once id from elements.

  • onceId: string — Unique identifier to remove
  • selector: string | Element | Iterable<Element> | ArrayLike<Element> — Elements to process
  • options: object (optional)
    • onceAttribute: string — Data attribute name (default: 'data-dom-once')
    • context: Document | DocumentFragment | Element — Query context (default: document)
  • Returns: Element[] — Only elements that actually had the once id removed (matched the selector AND had the once id). Unmarked elements are excluded.

findOnce(onceId[, options]) → Element[]

Finds all elements marked with a specific once id (read-only).

  • onceId: string — Unique identifier to search for
  • options: object (optional)
    • onceAttribute: string — Data attribute name (default: 'data-dom-once')
    • context: Document | DocumentFragment | Element — Search context (default: document)
  • Returns: Element[] — Elements marked with the once id

version

string — Library version (e.g., "1.0.0")

Development

This project uses pnpm for package management.

pnpm install          # Install dependencies
pnpm run check        # Lint, typecheck, and test
pnpm run build        # Build for production
pnpm run test         # Run tests

License

MIT © 2025 Brian T. Bailey