Categories
JavaScript Answers jQuery

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *