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 🙏

© 2024 – Pkg Stats / Ryan Hefner

select-by

v1.0.1

Published

A tiny, expressive yet concise JavaScript DOM selector library

Downloads

5

Readme

SelectBy

SelectBy aims to be a concise yet expressive DOM selector library that takes inspiration from jQuery's ability to shorthand DOM element selections. Where SelectBy differs is that each DOM selector makes use of methods which describe the selection taking place, making the intention clear and also taking the hassle out of writing out the lengthy full vanilla JavaScript selector.

Landing Pages

npm:
https://www.npmjs.com/package/select-by

jsdelvr:
https://www.jsdelivr.com/package/gh/nazarja/select-by

Installation

npm module / browser module

Node

npm install select-by

  • commonjs
    const by = require('select-by')
  • ecmascript modules
    import by from 'select-by'
Browser - place above your custom script files

jsdelvr

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/select-by.min.js"></script>

Return Values

Arrays are returned for operations that return multiple values and so they can be easily used with many array protoype methods.

Only 4 return values are returned throughout all the methods available.

  1. null : no result on single result selectors
  2. a singular element : results on singular result selectors
  3. an empty array : no results on multiple result selectors
  4. an array of elements : results on multiple result selectors

Example Usage

const score = by.id('score') <-- returns an element with the id of 'score'

Methods

QuerySelector

q(query: string): HTMLElement | null;

 by.q('#myId')
 by.q('.myClassname')

parameters: css style selector
return value: an element or null

QuerySelectorAll

qAll(query: string): Array<HTMLElement>;

 by.qAll('.item')

parameters: css style selector
return value: an array of elements or an empty array

Select a single element by id

id(query: string): HTMLElement | null;

 by.id('myId')

parameters: an element id
return value: an element or null

Select elements by class name

class(query: string): Array<Element>;

 by.class('myClassname')

parameters: an element class name
return value: an array of elements or an empty array

Select elements by tag name

tag(query: string): Array<Element>;

 by.tag('div')

parameters: an element tag name
return value: an array of elements or an empty array

Select elements by their name attributes value

name(query: string): Array<Element>;

 by.name('username')

parameters: an element name attribute value
return value: an array of elements or an empty array

Select elements by attribute name, optionally accepts a parent element selector

att(attribute: string, query?: string): Array<HTMLElement>;

 by.att('data-item')
 by.att('data-item="fruit"')
 by.att('href', '#contact')

parameters: html attribute selector [can include optional] attribute value, css style selector
return value: an array of elements or an empty array

Select all elements within the body tag, except script tags

all(): Array<HTMLElement>;

 by.all()

parameters: none
return value: an array of elements or an empty array

Select all elements with no children and no text content

empty(): Array<HTMLElement>;

 by.empty()

parameters: none
return value: an array of elements or an empty array

Select the first element of its type found

first(query: string): HTMLElement | null;

 by.first('.item')

parameters: css style selector
return value: an element or null

Select the last element of its type found

last(query: string): HTMLElement | null;

 by.last('.item')

parameters: css style selector
return value: an element or null

Select the parent element of the given element

parent(query: string): HTMLElement | null;

 by.parent('.item')

parameters: css style selector
return value: an element or null

Select the first child of the given element

firstChild(query: string): Element | null;

 by.firstChild('#container')

parameters: css style selector
return value: an element or null

Select the last child of the given element

lastChild(query: string): Element | null;

 by.lastChild('#container')

parameters: css style selector
return value: an element or null

Select all children of the given element

children(query: string): Array<Element>;

 by.children('#container')

parameters: css style selector
return value: an array of elements or an empty array

Select the next sibling of the given element

next(query: string): Element | null;

 by.next('#logo')

parameters: css style selector
return value: an element or null

Select the previous sibling of the given element

prev(query: string): Element | null;

 by.prev('#menu')

parameters: css style selector
return value: an element or null

Select the child element of the given element by index

index(query: string, index: number): Element | null;

 by.index('#container', 0)

parameters: css style selector, number
return value: an element or null

Select all elements within a range of the given element, optionally accepts a range end parameter

range(query: string, start: number, end?: number): Array<HTMLElement>;

 by.range('.item', 3)
 by.range('.item', 0, 2)

parameters: css style selector, number, [optional] number
return value: an array of elements or an empty array

Select all elements containing the given text, optionally accepts a parent element

text(text: string | number, query?: string): Array<HTMLElement>;

 by.text('Apple')
 by.text('Apple', 'li')
 by.text('Apple', '.item')

parameters: string [case sensitive] or number, [optional] css style selector
return value: an array of elements or an empty array