How to Get Child Element by Class in JavaScript

Mehvish Ashiq Feb 02, 2024
  1. Get Child Element by class in JavaScript
  2. Use querySelector() to Get Child Element by class Name in JavaScript
  3. Use querySelectorAll() Method to Get All Child Nodes Having the Same class in JavaScript
  4. Use document.getElementById() With Child Nodes’ Properties in JavaScript
  5. Use getElementsByClassName() Method to Get One or Multiple Child Elements by class Name in JavaScript
  6. Use :scope to Get Direct Children in JavaScript
How to Get Child Element by Class in JavaScript

This tutorial shows how to get child elements by class using JavaScript. It also highlights how to access one child element (first or last child element) or multiple child elements using their class name.

Let’s start learning them sequentially.

Get Child Element by class in JavaScript

We can use the following ways to get one or multiple child elements by class name in JavaScript.

  1. Use querySelector() method.
  2. Get all child nodes having the same class using querySelectorAll().
  3. Use document.getElementById() with Child Nodes’ Properties.
  4. Use getElementsByClassName() method.
  5. Use :scope to get direct children.

Use querySelector() to Get Child Element by class Name in JavaScript

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Get Child Elements</title>
</head>
<body>
  <ul id="parent-element">
    <li class="child-element-one">One</li>
    <li class="child-element-two">Two</li>
	<li class="child-element-three">Three</li>
    <li class="child-element-four">Four</li>
  </ul>
</body>
</html>

JavaScript Code:

var parent = document.querySelector('#parent-element');
var firstChild = parent.querySelector('.child-element-one');
var lastChild = parent.querySelector('.child-element-four');

document.writeln(
    'The innerHTML of First Child Element is "' + firstChild.innerHTML + '"' +
    ' and The innerHTML of Last Child Element is "' + lastChild.innerHTML +
    '"');

Output:

get child elements by class using javascript - output one

We first use the querySelector() method to get the parent element. Then, call this method again on the selected parent element to get the first and last child nodes that match the specified selector.

You may have observed that the querySelector() is scoped to a particular element here (that has the id parent-element). This is why it only chooses the child of the element it is called.

We can also use document.getElementById() to select the parent element. See the following example where we are selecting all child elements.

Use querySelectorAll() Method to Get All Child Nodes Having the Same class in JavaScript

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Get Child Elements</title>
</head>
<body>
  <ul id="parent-element">
    <li class="child-element">One</li>
    <li class="child-element">Two</li>
	<li class="child-element">Three</li>
    <li class="child-element">Four</li>
  </ul>
</body>
</html>

JavaScript Code:

var parent = document.getElementById('parent-element');
var allChildElements = parent.querySelectorAll('.child-element');

for (i = 0; i < allChildElements.length; i++)
  document.writeln(allChildElements[i].innerHTML);

Output:

get child elements by class using javascript - output two

The querySelectorAll() method selects all child nodes with the same class name. It returns the node list and shows SYNTAX_ERR in case of any invalid selector(s).

Use document.getElementById() With Child Nodes’ Properties in JavaScript

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Get Child Elements</title>
</head>
<body>
  <ul id="parent-element">
    <li class="child-element-one">One</li>
    <li class="child-element-two">Two</li>
	<li class="child-element-three">Three</li>
    <li class="child-element-four">Four</li>
  </ul>
</body>
</html>

JavaScript Code:

var parent = document.getElementById('parent-element');
var firstChild = parent.firstElementChild;
var lastChild = parent.lastElementChild;

document.writeln(
    'The innerHTML of First Child Element is "' + firstChild.innerHTML + '"' +
    ' and The innerHTML of Last Child Element is "' + lastChild.innerHTML +
    '"');

Output:

get child elements by class using javascript - output three

Here, the firstElementChild returns the first child element node and the lastElementChild returns the last child element node. We can also get all child Element nodes as follows.

JavaScript Code:

var parent = document.getElementById('parent-element');
var allChild = parent.children;

for (i = 0; i < allChild.length; i++) document.writeln(allChild[i].innerHTML);

Output:

get child elements by class using javascript - output four

Use getElementsByClassName() Method to Get One or Multiple Child Elements by class Name in JavaScript

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Get Child Elements</title>
</head>
<body>
  <ul id="parent-element">
    <li class="child-element">One</li>
    <li class="child-element">Two</li>
	<li class="child-element">Three</li>
    <li class="child-element">Four</li>
  </ul>
</body>
</html>

JavaScript Code:

var childs = (document.getElementById('parent-element'))
                 .getElementsByClassName('child-element');

for (i = 0; i < childs.length; i++) document.writeln(childs[i].innerHTML);

Output:

get child elements by class using javascript - output five

The getElementsByClassName() is a read-only property that helps to get all child nodes having the same class name.

Use :scope to Get Direct Children in JavaScript

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Get Child Elements</title>
</head>
<body>
  <ul id="parent-element">
    <li class="child-element-one">One</li>
    <li class="child-element-two">Two</li>
	<li class="child-element-three">Three</li>
    <li class="child-element-four">Four</li>
  </ul>
</body>
</html>

JavaScript Code:

var parent = document.getElementById('parent-element');
var childs = parent.querySelectorAll(':scope > li');

for (i = 0; i < childs.length; i++) document.writeln(childs[i].innerHTML);

Output:

get child elements by class using javascript - output six

We are using a pseudo-class named :scope, which is very useful for getting the direct children, which means the direct descendant of an element that we have already accessed.

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

Related Article - JavaScript Element