1.1.0 • Published 7 years ago
dom-query-helpers v1.1.0
dom-query-helpers
A super tiny DOM query helper library.
Installation
npm install dom-query-helpersNote: This library is written as ES2015 code and published as such to
npm.
That means, code from dom-query-helpers must not be excluded from
transpilation.
If you're using webpack and babel, that could look like:
{
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules\/(?!dom-query-helpers)/,
loader: 'babel-loader'
}
]
}
}Usage
import { closest, matches, query } from 'dom-query-helpers';closest()
closest(element: Element, selector: string): ElementReturns the closest ancestor of the element (or the element itself) which
matches the specified selector.
If there isn't such an ancestor, it returns null.
Example
const closestParagraph = closest(element, 'p');matches()
matches(element: Element, selector: string): booleanReturns true if the element would be selected by the specified selector,
false otherwise.
Example
const isParagraph = matches(element, 'p');query()
query(selector: string[, element: Element]): arrayReturns an array of elements matching the specified selector which are
descendants of the document or the element specified as optional second
argument.
Example
const paragraphs = query('p');
const spansInsideFirstParagraph = query('spans', paragraphs[0]);License
Copyright (c) 2018 Jan Sorgalla. Released under the MIT license.