• 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:

  • Get and Set Classes On An Element
  • Retrieve the value of a CSS rule for the specified element
  • Toggle a Class of an Element
  • Add, remove, toggle, and check for the presence of a class

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