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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With