Open In App
Related Articles

How to count all elements within a div using jQuery ?

Improve Article
Save Article
Like Article

In this article, we will count all elements inside a div element using jQuery. To count all elements inside a div elements, we use find() method and length property.

The find() method is used to find all the descendant elements of the selected element. It will traverse all the way down to the last leaf of the selected element in the DOM tree.



Here selector is the selected elements of which all the descendant elements are going to be found.

The length property is used to count number of the elements of the jQuery object.



Note: Here selector is the object whose length is to be calculated.



<!DOCTYPE html>
<html lang="en">
    <!-- Import jQuery cdn library -->
    <script src=
        $(document).ready(function () {
            $("button").click(function () {
                var count = $(".GFG").find("*").length;
                $("p").after("<h3>Total Div Elements: "
                    + count);
<body style="text-align: center;">
    <h1 style="color: green;">
        How to count all elements
        within a div using jQuery?
    <div class="GFG">
            GeeksforGeeks computer
            science portal
        <input type="text">
    <button>Click Here!</button>


Before Click Button:

After Click Button:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 07 Dec, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials