-2

Possible Duplicate:
Count elements with jQuery

I have a list item like this:

<ul id="scale">
   <li class="floatleft active"></li>
   <li class="floatleft active"></li>
   <li class="floatleft"></li>
   <li class="floatleft"></li>
   <li class="floatleft"></li>
</ul>

So what happens now is that through jQuery I set a couple of these list items as active. Now what I would like to do is count these active items on the same page after they have been actived. Is there any way to do that?

2
  • 4
    You might consider working through a beginners tutorial. Commented Jan 25, 2012 at 17:53
  • Sorry, I asked my question also wrong. I have edited my question. Commented Jan 25, 2012 at 18:00

5 Answers 5

9

You can use:

 $('#scale li.active').length
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that should be the solution. However, I would like to be able to count the active li items after I have actived them myself through jQuery on the same page. What happens now is that the value is the original value of active classes and not the actual. Is there a way to solve that?
Any time you run that will give you a count of the number of LI's in "scale" that have the class active. If you remove the "active" class from all, the length will be 0. The only catch is if you assigned the result to a varialbe, e.x: var items = $('#scale li.active'); alert(items.length); Any time you access "items" it will always reflect what the state was when the query was run.
2
document.getElementById('scale').getElementsByClassName('active').length

Comments

0
var count = $('#scale li.active').length;

alert(count);

This will give you the count of your active list items.

Comments

0

Similar question

$('#scale li.active').length
$('#scale li.active').size()

Comments

0

If you are using jQuery, then you can have something like this:

$('#scale li.active').length //efficient way

or

$('#scale li.active').size()

Both of them will give you the require result.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.