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

  • Get all of an element’s parent elements up the DOM tree
  • Check if HTML element has class
  • Get and set inline styles for an element
  • Selecting Elements By Class Name

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us