• jQuery Video Tutorials

jQuery length Property



The length property in jQuery is used to count the number of elements in the jQuery object. It returns an integer representing the number of matched elements. This property is read-only, meaning you can use it to get the count but not set it.

Syntax

Following is the syntax of jQuery length property −

$(selector).length

Parameters

The "selector" is a string containing a selector expression to match elements against.

Example

In the following example, we are demonstrating the basic usage of the jQuery length property −

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            var paragraphs = $("p");
            var count = paragraphs.length;
            $("div").text("There are " + count + " paragraph(s) on this page.");
        });
    </script>
</head>
<body>
    <p>First paragraph.</p>
    <p>Second paragraph.</p>
    <p>Third paragraph.</p>
    <div></div>
</body>
</html>

After executing the above program, It returns number of paragraph elements in the jQuery object.

jquery_ref_properties.htm
Advertisements