1

I have a div that contain many images:

<div id="#preview_images_gallery">
 <img class="image_url pager" src="image1.jpg">
 <img class="image_url pager" src="image2.jpg"> 
 <img class="image_url pager" src="image3.jpg">
 .
 .
 .
</div>

how I can know the number of images contained in this div with jquery?

hyperrjas
  • 10,666
  • 25
  • 99
  • 198

5 Answers5

6
$("#preview_images_gallery > img").size()

on second thoughts

  $("#preview_images_gallery > img").length
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • better than @Rory's only because it specifies immediate img children. – Dave G Feb 28 '12 at 14:27
  • 1
    from the jQuery docs : The .size() method is functionally equivalent to the .length property; however, the .length property is preferred because it does not have the overhead of a function call. – Manse Feb 28 '12 at 14:27
  • +1, better than mine, although the OPs `id` already contains a `#` so the selector here is incorrect. – Rory McCrossan Feb 28 '12 at 14:29
  • @RoryMcCrossan true, but I reckon the op should change not me ... :-) – NimChimpsky Feb 28 '12 at 14:30
1
$('#preview_images_gallery > img').length
Chris Dixon
  • 9,147
  • 5
  • 36
  • 68
1
var imagecount = $('#preview_images_gallery img').length

the length property of a jQuery collection.

Note : you should change your div id from #preview_images_gallery to preview_images_gallery

Manse
  • 37,765
  • 10
  • 83
  • 108
1
alert($("#preview_images_gallery img").length);//return numbers of images inside preview_images_gallery 
mgraph
  • 15,238
  • 4
  • 41
  • 75
1

This is how you can count the img, in plain javascript way :

var get=document.getElementById('preview_images_gallery');
var allget=get.getElementsByTagName('img');
alert(allget.length);
mgraph
  • 15,238
  • 4
  • 41
  • 75
Kunal Vashist
  • 2,380
  • 6
  • 30
  • 59