• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / CSS / Get the Parent of an Element

Get the Parent of an Element

Get the Parent of an Element

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/

CSS

Related Snippets:

  • Remove a class from HTML element
  • Remove a Class from an Element
  • Get the computed style of an element
  • Get the default value of a CSS property with JavaScript

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers