How to get the total number of elements selected with jQuery?

Sometimes, we want to get the total number of elements selected with jQuery.

In this article, we’ll look at how to get the total number of elements selected with jQuery.

How to get the total number of elements selected with jQuery?

To get the total number of elements selected with jQuery, we can use the length property.

For instance, if we have:

<div>
  foo
</div>
<div>
  bar
</div>
<div>
  baz
</div>

Then we write:

console.log($('div').length)

to log the number of divs selected, which is 3.

Conclusion

To get the total number of elements selected with jQuery, we can use the length property.