Reputation: 25
I have some problem in getting the length of the div with particular class.
HTML
<div class="sampleDiv">
<div class="e"></div>
<div class="e"></div>
<div class="list-tasks12">
<div class="slot"></div>
</div>
<div class="list-tasks12">
<div class="slot"></div>
<div class="slot"></div>
<div class="slot"></div>
</div>
</div>
JS
alert($(".sampleDiv").find('.list-tasks12:nth-child(1)').find('.slot').length)
Here it is showing length as zero which is wrong.
Please help what i am doing wrong
Upvotes: 1
Views: 1025
Reputation: 1465
The alert is not working properly because the <div class="e"></div>
are also children of the parent div. You should remove them from the <div class="sampleDiv">
container or count them also - so if you want the first child <div class="list-tasks12">
you should count the two <div class="e"></div>
containers and use the following script:
alert($(".sampleDiv").find('.list-tasks12:nth-child(3)').find('.slot').length);
For the second <div class="list-tasks12">
child you should use:
alert($(".sampleDiv").find('.list-tasks12:nth-child(4)').find('.slot').length);
Here is a jsfiddle for it: https://jsfiddle.net/b1hsp30j/
Upvotes: 1
Reputation: 146
Try this
$(".sampleDiv").find('.list-tasks12').eq(0).find('.slot').length
Upvotes: 1