• 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 Children of an Element

Get the Children of an Element

Get the Children of an Element

To get the children of an element, you use the childNodes property of the element.

The following selects the children of the element whose id is container:

const container = document.querySelector('#container');
const children = container.childNodes;

How it works:

  • First, select the element whose id is container using the querySelector() method.
  • Then, use the childNodes property to get the children of the container elements.

To get the first and last children, you use the firstChild and lastChild properties as follows:

const container = document.querySelector('#container');

const firstChild = container.firstChild,
      lastChild = container.lastChild;

Source

https://www.javascripttutorial.net/dom/traversing/get-the-children-of-an-element/

CSS

Related Snippets:

  • Selecting Elements By Class Name
  • Remove a Class from an Element
  • Retrieve the value of a CSS rule for the specified element
  • Toggle a class for an HTML element

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