How to calculate the different number of blocks with the same classes?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
8 views
Hello! Friends, asking for help, did not get to implement his plan.
Here is a simple example:


Initially the blocks are not. There is a button which when pressed creates a block .main in this block there is another button that, when clicked, create child blocks .div.

How to count elements with class="div" in each block class="main", for example, by their numbering.
For example the blocks are created dynamically and would like them to assign a sequence number. I understand that you need to act on the indication of the object this, but I can't figure this out :(

At the moment, it counts the number of all elements:
$('.main .div').length;

Thank you for your attention and help!
by | 8 views

2 Answers

0 like 0 dislike
$('.main').each(function(){ alert($(this).find('.div').length); })
by
0 like 0 dislike
var elems = document.querySelectorAll('.main'); [].forEach.call(elems, function(elem) { console.log( elem.children.length ); });

Or so
CSS counters
by

Related questions

110,608 questions
257,186 answers
0 comments
35,406 users