user5309698
user5309698

Reputation: 25

How to get length of div with particular class

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

Demo Here

Upvotes: 1

Views: 1025

Answers (2)

Viktor Maksimov
Viktor Maksimov

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

satishkumar
satishkumar

Reputation: 146

Try this

$(".sampleDiv").find('.list-tasks12').eq(0).find('.slot').length

Upvotes: 1

Related Questions