Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of img tags inside a div tag

Tags:

jquery

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?

like image 359
aBhijit Avatar asked Jul 24 '13 12:07

aBhijit


2 Answers

Count img inside #some_div:

 $("#some_id img").length

If you want only the direct children, not all descendants:

$("#some_id > img").length
like image 92
jods Avatar answered Oct 29 '22 21:10

jods


Use

var count = $("#some_id").find('img').length;
like image 20
Optimus Prime Avatar answered Oct 29 '22 22:10

Optimus Prime