This snippet gets the parent of an element by using the parentNode
property of the element.
The following code gets the parent node of the element with the id main
:
const current = document.querySelector('#main');
const parent = current.parentNode;
How it works:
- First, select the element with the CSS selector #main
- Then, use the parentNode property to get the parent element of the selected element.
Source
https://www.javascripttutorial.net/dom/traversing/get-the-parent-of-an-element/