Lai32290
Lai32290

Reputation: 8548

How to check element is child of specific element?

If I get elementA and elementB with document.querySelector, how to can I check if elementA is a child element of elementB without jQuery?

Considering elementA maybe not first level child of elementB.

Exemple: http://jsbin.com/jijoqicozo/edit?html,js,output

Upvotes: 1

Views: 60

Answers (1)

Derek
Derek

Reputation: 557

You can use B.contains(A) to determine if element A is a descendent of element B.

Note that node.contains is inclusive, meaning that B.contains(B) === true

https://developer.mozilla.org/en/docs/Web/API/Node/contains

Upvotes: 4

Related Questions