An open API service indexing awesome lists of open source software.

https://github.com/chrisyip/closest-element

Find the closest element that matches a selector or an element
https://github.com/chrisyip/closest-element

Last synced: about 1 year ago
JSON representation

Find the closest element that matches a selector or an element

Awesome Lists containing this project

README

          

# closest-element

Find the closest element that matches a selector or an element

## Usage

```js
closest(element, selectorOrElement, [rootElement = null, [excludeSelf = false]])
```

`element`, the first element.

`selectorOrElement`, a string of selector expression or an element to match elements against.

`rootElement`, if matches current element, this function will stop traversing up.

`excludeSelf`, whether skip self from matching.

Returns matched element, or `null` if no element found.

Example:

```html

```

```js
import closest from 'closest-element'

closest(document.querySelector('ul'), 'ul') // returns


    closest(document.querySelector('ul'), 'ul', true) // returns null
    closest(document.querySelector('a'), 'ul li', document.querySelector('li')) //

  • closest(document.querySelector('a'), 'ul', document.querySelector('li')) // false
    closest(document.querySelector('a'), document.querySelector('nav')) //
    ```

    ### Files

    ```
    src/
    - cloest.js // in es6

    dist/
    - closest.cjs.js // CMD
    - closest.es6.js // ES6 module, compiled with Buble
    - closest.js // UMD
    ```