1.1.0 • Published 6 years ago

dom-query-helpers v1.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

dom-query-helpers

A super tiny DOM query helper library.

Build Status

Installation

npm install dom-query-helpers

Note: 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): Element

Returns 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): boolean

Returns 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]): array

Returns 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.