Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of Divs filtering on id/class

Tags:

jquery

Hiii,
Q-> I want to count the number of div in a page whose id start with d

<div id="d1">D1</div>
<div id="d2">D2</div>
<div id="d3">D3</div>

I want to get 3 here


Q-> I want to count the number of div in a page which have the class d

<div class="d">D1</div>
<div class="d">D2</div>

I want to get 2 here

like image 871
Sourav Avatar asked Jun 24 '11 02:06

Sourav


1 Answers

For the first one you just need a "starts with selector"

$('div[id^=d]').length

And the second is just a standard class selector:

$('div.d').length

The $() function call returns an array of matching elements so once you have it, you just need to look at the length property to find out how many matches there are.

like image 195
mu is too short Avatar answered Nov 15 '22 17:11

mu is too short