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
- Host: GitHub
- URL: https://github.com/chrisyip/closest-element
- Owner: chrisyip
- Created: 2016-06-18T15:50:26.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-08-11T02:46:49.000Z (almost 9 years ago)
- Last Synced: 2025-05-09T14:05:42.948Z (about 1 year ago)
- Language: JavaScript
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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('a'), 'ul', document.querySelector('li')) // false
closest(document.querySelector('a'), document.querySelector('nav')) //
```### Files
```
src/
- cloest.js // in es6dist/
- closest.cjs.js // CMD
- closest.es6.js // ES6 module, compiled with Buble
- closest.js // UMD
```
closest(document.querySelector('ul'), 'ul', true) // returns null
closest(document.querySelector('a'), 'ul li', document.querySelector('li')) //