# dom-parents

> Get the parents of the element, optionally filtered by a selector.

Latest version **1.1.4** (published 2023-12-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install dom-parents
pnpm add dom-parents
yarn add dom-parents
bun add dom-parents
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.4 |
| Published | 2023-12-05 |
| First published | 2018-02-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Sergei Marochkin |
| Maintainers | ziggi |
| Keywords | parents, dom-parents |

## Links

- npm: https://www.npmjs.com/package/dom-parents
- Repository: https://github.com/ziggi/dom-parents
- Homepage: https://github.com/ziggi/dom-parents#readme
- Issues: https://github.com/ziggi/dom-parents/issues
- npm.io page: https://npm.io/package/dom-parents

## Recent versions

- 1.1.4 (latest) — 2023-12-05
- 1.1.3 — 2023-10-18
- 1.1.2 — 2023-10-18
- 1.1.1 — 2018-08-22
- 1.1.0 — 2018-08-22
- 1.0.2 — 2018-02-23
- 1.0.1 — 2018-02-23
- 1.0.0 — 2018-02-23

## README

# dom-parents

Get the parents of the element, optionally filtered by a selector.

## Install

```sh
npm install --save dom-parents
```

## Examples

### Apply action to all parent elements

```js
import getParents from 'dom-parents';

getParents(document.querySelector('#main'), '.cat').forEach((element) => {
  element.style.backgroundColor = '#008800';
})
```

### Check the existence of parents

```js
import getParents from 'dom-parents';

document.querySelectorAll('.animal').forEach((element) => {
  element.addEventListener('mousedown', () => {
    const isBobAnAnimal = getParents(this, '.animals').length !== 0;
    if (isBobAnAnimal) {
      console.log('Bob is animal');
    } else {
      console.log('Bob is spy!');
    }
  });
});
```

### If the user clicks on an `.item` element
```js
import getParents from 'dom-parents';

document.addEventListener('mousedown', (event) => {
  const [item] = getParents(event.target, '.item', true);
  if (item) {
    console.log('mousedown on .item element');
  }
});
```

## API

### getParents(element, selector, includeElement = false)

Returns the parents of the element, optionally filtered by a selector.

#### element

Type: `object`

The element from which the search should start.

#### selector

Type: `string`

Selector to search for the parent elements.

#### includeElement

Type: `bool`

Default: `false`

Include `element` to the search or not.

---
_Source: https://npm.io/package/dom-parents · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
