Zak
Zak

Reputation: 2698

Is there a way to get the number of elements in a class using jQuery?

I'm trying to write a simple jQuery slider, and I need to find the number of elements with a particular class. Is there a way to do this?

Upvotes: 2

Views: 78

Answers (3)

James Sumners
James Sumners

Reputation: 14777

var $elements = $('.classname'); // cache the result of your query
var numberOfElements = $elements.length; // slightly faster than .size()

If you use the .length property you will skip a function call. The size() function merely returns this.length.

Upvotes: 3

Felix Kling
Felix Kling

Reputation: 816404

Use the length property:

$('.yourClass').length

Upvotes: 1

Pekka
Pekka

Reputation: 449415

$(".classname").size()

should do the trick.

jQuery docs on .size()

Upvotes: 2

Related Questions