I am trying to count how many element with the class .test within my div. I have
<div id='testDiv'>ddd
<div class='test'> 111<div> another div </div></div>
<div class='test'> 222</div>
<div class='test'> 333</div>
more...
</div>
My jQuery:
if($('#testDiv.test').length>5){
alert('haha');
}
I can’t seem to get the correct number of div with the class name test.
Change the selector like following:
Put an
spacebetween#testDivand.test.$('#testDiv .test')will find out the direct children of#testDivwho hasclass=test.Full Code:
Note
$('#testDiv.test')means you are selection some element which has bothid=testDivandclass=testlike<div id="testDiv" class="test"></div>.