# Check if an element is a descendant of another

True when child is inside parent, at any depth.

Canonical: https://1loc.dev/dom/check-if-an-element-is-a-descendant-of-another/
Version: 1.0.0
Runtime: JavaScript · modern browsers (DOM)
Verification: Usage lines executed in headless Chromium against the fixture markup; no external services
Updated: 2026-10-28

## Implementation

```javascript
function isDescendant(child, parent) { return child !== parent && parent.contains(child); }
```

## Usage

Markup:

```html
<ul id="list"><li id="item">Item</li></ul>
```

Script:

```javascript
const list = document.querySelector('#list');
isDescendant(document.querySelector('#item'), list); // → true
isDescendant(list, list); // → false
list.contains(list); // → true
```

Lines marked → were executed in headless Chromium against the markup above, and each result was compared with the value shown. Lines marked e.g. are illustrative.

## Limits

- Node.contains returns true for the node itself, as the last line shows, so the original called an element its own descendant.
- contains does not cross shadow roots: an element inside a shadow tree is not a descendant of the host’s ancestors.

The function does not validate its arguments. Add checks where it receives untrusted input.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/check-if-an-element-is-a-descendant-of-another.mdx
Source file (this edition): https://github.com/akamind/1loc/blob/0a77f131b22d2ba3b7ec1728846f900c22821678/contents/check-if-an-element-is-a-descendant-of-another.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Excludes the element itself, which Node.contains includes. Executed in Chromium. Original MDX retained as inert source evidence.
