Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of elements with the same class- jquery

I have an ul element. now I would like to know how many of li's have selected class .

<ul class="icon-list icon-list-inline star-rating" id="star-rating">

        <li class="selected"></li>
        <li class=""></li>
        <li class=""></li>
        <li class=""></li>
        <li class=""></li>
</ul>


var vote_selected = $('#star-rating ... ???').length;

it must return 1 .

like image 994
S.M_Emamian Avatar asked Dec 08 '22 22:12

S.M_Emamian


1 Answers

Try this :-

var vote_selected = $('#star-rating li[class="selected"]').length;

DEMO

Or (as li is immediate descendents of #star-rating you can use >).

var vote_selected = $('#star-rating > li[class="selected"]').length;

Or

var vote_selected = $('#star-rating > .selected').length;
like image 97
Kartikeya Khosla Avatar answered Dec 10 '22 11:12

Kartikeya Khosla