Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of elements with the same class in jquery

Tags:

jquery

How do I count dynamically generate elements with the same class in jquery? I found a similar question, but unfortunately it didn't work. jQuery counting elements by class; what is the best way to implement this?

I did something like this which I based from the answers:

$('.capital_class').live('blur', function(){

alert($(this).length);
});

The elements with a class of capital_class are dynamically generated. But I always get the length of 1, whenever I blur. How do I get this correctly?

like image 415
Wern Ancheta Avatar asked Aug 21 '11 13:08

Wern Ancheta


1 Answers

Change the alert to this:

alert($('.capital_class').length);

Remember the event is called on a single element so this is just a single element -- you have to have jQuery query the dom after the event happens. (The first query just sets up the live handling to create the event handlers.)

like image 114
Hogan Avatar answered Sep 23 '22 18:09

Hogan