Lightweight querySelector/All wrapper that outputs an Array
MIT License
Lightweight
querySelector
/All
wrapper that outputs an Array
Version 7+ only supports browsers with iterable NodeList
s. If you need IE support, stick to select-dom@6
or lower.
npm install select-dom
// This module is only offered as a ES Module
import {$, $$, lastElement, elementExists} from 'select-dom';
Note: if a falsy value is passed as baseElement
, you'll always get an empty result (bd578b9)
$(selector[, baseElement = document])
Maps to baseElement.querySelector(selector)
, except it returns undefined
if it's not found
$('.foo a[href=bar]');
// => <Element>
$('.foo a[href=bar]', baseElement);
// => <Element>
$('.non-existent', baseElement);
// => undefined
lastElement(selector[, baseElement = document])
Like $()
, except that it returns the last matching item on the page instead of the first one.
elementExists(selector[, baseElement = document])
Tests the existence of one or more elements matching the selector. It's like $()
, except it returns a boolean
.
elementExists('.foo a[href=bar]');
// => true/false
elementExists('.foo a[href=bar]', baseElement);
// => true/false
$$(selector[, baseElements = document])
Maps to baseElements.querySelectorAll(selector)
plus:
baseElements
can be a list of elements to query$$('.foo');
// => [<Element>, <Element>, <Element>]
$$('.foo', baseElement);
// => [<Element>, <Element>, <Element>]
$$('.foo', [baseElement1, baseElement2]);
// => [<Element>, <Element>, <Element>]
// This is similar to jQuery([baseElement1, baseElement2]).find('.foo')
DocumentFragment
or one Element
, in a few bytes.