What is the size() method in jQuery?

Overview

The size() method in jQuery is used to find the number of elements that match the given jQuery selector.

Syntax

$(selector).size();

Parameters

This method doesn’t accept parameters.

Return value

This method returns the number of elements that match the given jQuery selector.

Example

Let’s look at an example of the size() method in the code snippet below:

Console

Explanation

In the HTML tab:

  • Lines 5 to 9: We create a 5 HTML div.
  • Line 7: We import the jQuery script.

Note: We import the older version.

In the JavaScript tab:

  • Line 3: We use the size() method to count the number of div.
  • Line 5: We output the numberOfDivs on the console.

Output

The size() method counts the number of elements that match the selector div and output the result to the console.

Note: This method was deprecated in the jQuery version ^1.8 and was removed in the jQuery version ^3.0.