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.
npm:
https://www.npmjs.com/package/select-by
jsdelvr:
https://www.jsdelivr.com/package/gh/nazarja/select-by
npm install select-by
- commonjs
const by = require('select-by') - ecmascript modules
import by from 'select-by'
jsdelvr
The by variable will be available at the global window level.
<script src="https://cdn.jsdelivr.net/npm/select-by@1.0.1/dist/select-by.min.js"></script>
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.
null: no result on single result selectorsa singular element: results on singular result selectorsan empty array: no results on multiple result selectorsan array of elements: results on multiple result selectors
const score = by.id('score') <-- returns an element with the id of 'score'
q(query: string): HTMLElement | null;
by.q('#myId')
by.q('.myClassname')parameters: css style selector
return value: an element or null
qAll(query: string): Array<HTMLElement>;
by.qAll('.item')parameters: css style selector
return value: an array of elements or an empty array
id(query: string): HTMLElement | null;
by.id('myId')parameters: an element id
return value: an element or null
class(query: string): Array<Element>;
by.class('myClassname')parameters: an element class name
return value: an array of elements or an empty array
tag(query: string): Array<Element>;
by.tag('div')parameters: an element tag name
return value: an array of elements or an empty array
name(query: string): Array<Element>;
by.name('username')parameters: an element name attribute value
return value: an array of elements or an empty array
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
all(): Array<HTMLElement>;
by.all()parameters: none
return value: an array of elements or an empty array
empty(): Array<HTMLElement>;
by.empty()parameters: none
return value: an array of elements or an empty array
first(query: string): HTMLElement | null;
by.first('.item')parameters: css style selector
return value: an element or null
last(query: string): HTMLElement | null;
by.last('.item')parameters: css style selector
return value: an element or null
parent(query: string): HTMLElement | null;
by.parent('.item')parameters: css style selector
return value: an element or null
firstChild(query: string): Element | null;
by.firstChild('#container')parameters: css style selector
return value: an element or null
lastChild(query: string): Element | null;
by.lastChild('#container')parameters: css style selector
return value: an element or null
children(query: string): Array<Element>;
by.children('#container')parameters: css style selector
return value: an array of elements or an empty array
next(query: string): Element | null;
by.next('#logo')parameters: css style selector
return value: an element or null
prev(query: string): Element | null;
by.prev('#menu')parameters: css style selector
return value: an element or null
index(query: string, index: number): Element | null;
by.index('#container', 0)parameters: css style selector, number
return value: an element or null
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
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