My code goes like this.
<div id="some_id">
<img src="some_image.png">
<img src="some_image.png">
<div class="another_div"></div>
<div class="another_div"></div>
</div>
I want to count number of img tags inside that div element.
I found this from a similar question on stackoverflow which returns count of all the children.
var count = $("#some_id").children().length;
How do I modify this code or use some other function to count the number of img tags inside the div?
Count img inside #some_div:
$("#some_id img").length
If you want only the direct children, not all descendants:
$("#some_id > img").length
Use
var count = $("#some_id").find('img').length;
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