DOM queries that always return an array.
Introduction
Modern browsers enable us to perform DOM queries natively, e.g:
let cookies = document;
But we all want to loop over the returned elements. So in practice, we’ve got to do a little more work, particularly converting the resulting NodeList
to an array, e.g:
let cookies;try let query = document; cookies = Arrayprototypeslice; catch err console; cookies;
Tealight provides a familiar API to perform native queries, without the extra work.
;
Installation
Browser
A simple and fast way to get started is to include this script on your page:
This will create the global variable tealight
.
Module
$ npm install tealight
CommonJS
const tealight = ;
ES2015
;
Usage
tealight
accepts a single argument target
and will always return an array of 0 or more DOM nodes.
For the examples below, we will query against this HTML fragment:
tealight(target: string): Array<HTMLElement>
string
targets will be used as CSS selectors.
;// => [ <div#jar> ]
;// => [ <div.chocolate-chip.cookie>, <div.peanut-butter.cookie>, <div.short-bread.cookie> ]
tealight(target: HTMLElement): Array<HTMLElement>
HTMLElement
targets will simply be wrapped in an Array
const node = document; ;// => [ <div#jar> ]
tealight(target: HTMLCollection) : Array<HTMLElement>
HTMLCollection
arguments will be converted to Array
.
const nodeList = document; ;// => [ <div.chocolate-chip.cookie>, <div.peanut-butter.cookie>, <div.short-bread.cookie> ]
tealight(target: Array<any>): Array<HTMLElement>
Array
targets will be filtered, leaving only HTMLElement
let node = document;let array = node null ".cookie"; ;// => [ <div#jar> ]
Copyright 2018 Fisssion LLC.
Open source under the MIT License.