javascript - selecting all elements of a class within elements of given id -
my html code looks
<div id="parta"> <div class="myclass typea"> blabla </div> <div class="myclass typea"> blabla </div> <div class="myclass typea"> blabla </div> <div class="myclass typeb"> blabla </div> </div> <div id="partb"> <div class="myclass typea"> blabla </div> <div class="myclass typea"> blabla </div> <div class="myclass typea"> blabla </div> <div class="myclass typeb"> blabla </div> </div>
i want use javascript count number of elements of class myclass
, 1 within #parta
.
when use
$(".myclass").size()
8
result (it counts elements both parta
, partb
).
when use
$(".myclass.typea").size()
6
(it gets myclass
, typea
elements, both parts)
when use
$("#parta.myclass").size()
0
.
apparently, javascript command looks elements have both id=parta
, class=myclass
. possible reformulate command in order count number of myclass
class contained within elements id=parta
?
thanks lot!
you're missing space in 1 of selectors. try:
$("#parta .myclass").size()
alternatively do:
$('#parta').find('.myclass').length;
or
var parta = $('#parta'); $('.myclass', parta).length;
Comments
Post a Comment