Skip to content
Related Articles
Open in App
Not now

Related Articles

How to count every element including head body in the document in jQuery ?

Improve Article
Save Article
  • Last Updated : 25 Jan, 2022
Improve Article
Save Article

In this article, we will count every element (including head, body, etc) in the document using jQuery. To count all HTML elements, we use length property. The length property is used to count number of the elements of the jQuery object. 



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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!