get-elements-array
v1.0.1
Published
Array of elements from selector, Array-like objects and single elements
Downloads
13
Maintainers
Readme
get-elements-array
Array of elements from selector, Array-like objects and single elements.
The purpose of this module is to simplify the interpretation of an "elements" parameter in your own functions/modules, kinda like jQuery does.
Usage
getElements()
always returns a plain Array of elements with many types of input:
// a selector string:
elementsArray = getElements('.article img');
// => [<img>, <img>, <img>]
// a single DOM element:
elementsArray = getElements(document.body);
// => [document.body]
// a collection of elements:
elementsArray = getElements(document.body.children);
// => [<h1>, <p>, ...]
// an array of elements:
elementsArray = getElements([document.body, document.head]);
// => [document.body, document.head]
// an empty collection or null parameter:
elementsArray = getElements(undefined);
// => []
// `document` or `window:
elementsArray = getElements(document);
// => [document]
Note: it does not verify that the resulting array contains only elements. If needed, you can use a filter on the now-easy-to-use array:
const getElements = require('get-elements-array');
const isElement = require('lodash.iselement');
const elements = getElements([1, document.body]).filter(isElement);
// => [document.body]
Install
Pick your favorite:
<script src="dist/get-elements-array.browser.js"></script>
npm install --save get-elements-array
var getElements = require('get-elements-array');
import getElements from 'get-elements-array';
Dependencies
None!
Related
- on-off: Add and remove multiple events on multiple elements in <1KB
License
MIT © Federico Brigante