jmasterx
jmasterx

Reputation: 54113

Count of elements in a div with certain class?

I have this:

$('.project-header').html('Projects (' + numProjects + ')');
$('.task-header').html('Tasks (' + numTasks + ')');
$('.case-header').html('Cases (' + numCases + ')');

Where numProjects should be for example:

$('.project-header').numElementsWith('.external-element');

Is there some way to get the number of elements matching the class like this?

Thanks

Upvotes: 1

Views: 78

Answers (4)

Anujith
Anujith

Reputation: 9370

Just:

$('.project-header .external-element').length;

Upvotes: 0

paulslater19
paulslater19

Reputation: 5917

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>


$("ul").children().length // 3

Upvotes: 0

epascarello
epascarello

Reputation: 207511

Use length!

var count = $('.project-header').find('.external-element').length;

Upvotes: 4

Marcus
Marcus

Reputation: 5447

$('.project-header .external-element').length;

Upvotes: 0

Related Questions