To get the parent of an element, you use 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;
Code language: JavaScript (javascript)
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.
Was this tutorial helpful ?