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

select-dom

v10.1.0

Published

Lightweight querySelector/All/closest wrapper that returns an array and optionally throws if elements are not found

Readme

select-dom npm downloads

Lightweight querySelector/All/closest wrapper that returns an array and optionally throws if elements are not found

Install

npm install select-dom
import {
	$,
	$$,
	lastElement,
	elementExists,
	assertElementExists,
	$optional,
	$$optional,
	lastElementOptional,
	$closest,
	$closestOptional,
} from 'select-dom';

The package also includes an ESLint plugin entry point at select-dom/eslint-plugin; read documentation.

API

Note: if a falsy value is passed as baseElement, $, $$, lastElement throw ElementNotFoundError, while $optional, $$optional, lastElementOptional return undefined/[] (bd578b9)

$

$optional

$ maps to baseElement.querySelector(selector), except it throws ElementNotFoundError if no element is found. $optional returns undefined instead of throwing.

$('.foo a[href=bar]');
// => <Element>

$('.non-existent');
// throws ElementNotFoundError

$optional('.non-existent');
// => undefined

$$

$$optional

$$ maps to baseElements.querySelectorAll(selector) and always returns an array. baseElements can also be an array of elements to search within. Throws ElementNotFoundError if no elements are found. $$optional returns [] instead of throwing.

$$('.foo');
// => [<Element>, <Element>, <Element>]

$$('.foo', baseElement);
// => [<Element>, <Element>, <Element>]

$$('.foo', [baseElement1, baseElement2]);
// => [<Element>, <Element>, <Element>]

$$('.non-existent');
// throws ElementNotFoundError

$$optional('.non-existent');
// => []

lastElement

lastElementOptional

Like $/$optional, except they return the last matching element instead of the first.

lastElement('.foo');
// => <Element>

lastElement('.non-existent');
// throws ElementNotFoundError

lastElementOptional('.non-existent');
// => undefined

$closest

$closestOptional

Like element.closest(selector), except baseElement can be any Node including text nodes. $closest throws ElementNotFoundError if not found; $closestOptional returns undefined.

$closest('button', event.target);
// => <button>

$closest('button', button.firstChild); // text nodes are supported
// => <button>

$closest('.non-existent', element);
// throws ElementNotFoundError

$closestOptional('.non-existent', element);
// => undefined

elementExists

Tests the existence of one or more elements. Like $optional(), but returns a boolean.

elementExists('.foo a[href=bar]');
// => true/false

assertElementExists

Like elementExists(), but throws ElementNotFoundError instead of returning false.

assertElementExists('.foo a[href=bar]');
// => void (if element exists)
// => throws ElementNotFoundError (if element doesn't exist)

countElements

Counts the number of elements found. Shortcut for $$optional(selector).length.

countElements('a');
// => 3

ESLint

select-dom/eslint-plugin includes the select-dom/prefer rule, which autofixes .querySelector(), .querySelectorAll(), and .closest() calls to the matching select-dom helpers.

import selectDom from 'select-dom/eslint-plugin';

export default [
	{
		plugins: {
			'select-dom': selectDom,
		},
		rules: {
			'select-dom/prefer': 'error',
		},
	},
];

By default, the rule reports all supported calls. To keep readability exceptions like element.firstChild.querySelector(selector), enable allowReadabilityExceptions.

import selectDom from 'select-dom/eslint-plugin';

export default [
	{
		plugins: {
			'select-dom': selectDom,
		},
		rules: {
			'select-dom/prefer': ['error', {
				allowReadabilityExceptions: true,
			}],
		},
	},
];

Related

  • delegate-it - DOM event delegation, in <1KB.
  • doma - Parse an HTML string into DocumentFragment or one Element, in a few bytes.