Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. HTML Element .children / firstElementChild / lastElementChild

HTML Element .children / firstElementChild / lastElementChild

Since: DOM Level 1(1998)

Properties for retrieving child elements of an HTML element. You can get a list of child elements, or the first and last child element.

Syntax

// Get a list of child elements.
var elements = element.children;

// Get the first child element.
var first = element.firstElementChild;

// Get the last child element.
var last = element.lastElementChild;

Property List

PropertyDescription
childrenReturns only HTML element children as an HTMLCollection. Text nodes and comment nodes are not included.
childNodesReturns all child nodes, including text nodes and comment nodes, as a NodeList.
firstElementChildReturns the first child element (HTML element). Returns null if no child elements exist.
lastElementChildReturns the last child element (HTML element). Returns null if no child elements exist.
firstChildReturns the first child node. Text nodes and comment nodes are included.
lastChildReturns the last child node. Text nodes and comment nodes are included.

Sample Code

sample_children.html
<ul id="members">
	<li>Okabe Rintaro</li>
	<li>Makise Kurisu</li>
	<li>Shiina Mayuri</li>
	<li>Hashida Itaru</li>
</ul>
sample_children.js
var members = document.getElementById("members");

// Pattern 1: Check the count and list of child elements with children
console.log(members.children.length); // Outputs "4"

// Pattern 2: Access a specific child element by index
console.log(members.children[0].textContent); // Outputs "Okabe Rintaro"
console.log(members.children[3].textContent); // Outputs "Hashida Itaru"

// Pattern 3: childNodes includes text nodes, so the count is higher
console.log(members.childNodes.length); // Outputs "9" (newlines between tags are also counted)

// Pattern 4: Get only HTML elements reliably with firstElementChild / lastElementChild
console.log(members.firstElementChild.textContent); // Outputs "Okabe Rintaro"
console.log(members.lastElementChild.textContent);  // Outputs "Hashida Itaru"

// Pattern 5: Loop through all child elements
for (var i = 0; i < members.children.length; i++) {
	console.log((i + 1) + ": " + members.children[i].textContent);
}
// Outputs "1: Okabe Rintaro", "2: Makise Kurisu", "3: Shiina Mayuri", "4: Hashida Itaru" in order.

View sample page

Notes

element.children returns only HTML element children as an HTMLCollection. element.childNodes returns all child nodes, including text nodes and comment nodes, so for typical DOM manipulation, using element.children — which only includes HTML elements — helps avoid unexpected results.

In HTML source code, line breaks and indentation whitespace between tags are treated as text nodes. As a result, using childNodes, firstChild, or lastChild may return whitespace text nodes. When you need to reliably retrieve HTML elements only, use children, firstElementChild, or lastElementChild.

To count child elements, use children.length. If you need to find child elements matching a specific condition, element.querySelector() or element.querySelectorAll() are more convenient.

Browser Compatibility

Chrome Chrome
49+
Firefox Firefox
57+
2.5 or earlier ×
Safari Safari
18+
3.1 or earlier ×
Edge Edge
80+
11 or earlier ×
IE IE
11+
5 or earlier ×
Opera Opera
48+
9 or earlier ×
iOS Safari iOS Safari
18+
2 or earlier ×
Android Browser Android Browser
37+
4 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .