LightM1
LightM1

Reputation: 105

Count Images in a Div

I'm having issues count the number of images in a div.

For example 1/4, 2/4, 3/4, 4/4

This the small code I am trying to figuring out

//count image
var currentNum = $(this).iniShow() + 1;
var count = $(this).closest('.slideshow').find('img:first').length;
var item = $(this).closest('.slideshow').attr('id');
       $('.' + item ).html(currentNum + ' of ' + count);

Here is my html code so far:

<!DOCTYPE html> 
<html> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">   </script>
</head>
<style>
.slideshow img { display: none; cursor: pointer; }
</style>
<script type="text/javascript">
jQuery(function($){
//previous slide
$('.slideshow .prev').click(function() {
    prevSlide($(this).closest('.slideshow').find('.slides'));
});
//next slide
$('.slideshow .next, .slideshow img,').click(function() {
    nextSlide($(this).closest('.slideshow').find('.slides'));
});
//initialize show
iniShow();
function iniShow() {
    // show first slide with caption
    $('.slideshow').each(function() {
        showSlide($(this).find('.slides'));
    });
}
// move previous slide 
function prevSlide($slides) {
    $slides.find('img:last').prependTo($slides);
    showSlide($slides);
}
// move next slide 
function nextSlide($slides) {
    $slides.find('img:first').appendTo($slides);
    showSlide($slides);
}
// show slide with caption
function showSlide($slides) {
    var $nextSlide = $slides.find('img:first');
    //hide (reset) all slides
    $slides.find('img').hide();
    //fade in next slide
    $nextSlide.fadeIn(500);
    //show caption
    $('#caption').text($nextSlide[0].alt);
    //count image
    var currentNum = $(this).iniShow() + 1;
    var count = $(this).closest('.slideshow').find('img').length;
    $('#count').html(currentNum + ' of ' + count);
    }
});
</script>
<body>
<div class="slideshow">
<div class="slides">
    <img src="image/1a.jpg" width="500" height="500" alt="Caption 1 Image is ..." />
    <img src="image/2a.jpg" width="500" height="500" alt="Caption 2 Image is ..." />
    <img src="image/3a.jpg" width="500" height="500" alt="Caption 3 Image is ..." />
    <img src="image/4a.jpg" width="500" height="500" alt="Caption 4 Image is ..." />
</div>
<p id="caption"></p> / <p id="count"></p>
<div class="controls">
<a class="prev" href="javascript:void(0);">Previous</a> -
<a class="next" href="javascript:void(0);">Next</a>
</div>
</div>
</body>
</html>

Any suggestion would be appreciated. I also want to know am I close to solve this issue. Thanks.

Upvotes: 0

Views: 4695

Answers (4)

lolo
lolo

Reputation: 17864

You can try this:

var count = $(".slides > img").size();

Its better to use id and not class in your case. so your div will look like that:

  <div id="slides">
    <img src="image/1a.jpg" width="500" height="500" alt="Caption 1 Image is ..." />
    <img src="image/2a.jpg" width="500" height="500" alt="Caption 2 Image is ..." />
    <img src="image/3a.jpg" width="500" height="500" alt="Caption 3 Image is ..." />
    <img src="image/4a.jpg" width="500" height="500" alt="Caption 4 Image is ..." />
  </div>
...

And your jquery code:

var count = $("#slides > img").size();

see example: jsfiddle

Upvotes: 0

Anoop
Anoop

Reputation: 23208

You can directly use count as selector. changes in jsfiddle

for

var item = $(this).closest('.slideshow').attr('id');
       $('.' + item ).html(currentNum + ' of ' + count);

I believe you want to show your count in div with id count. for id you should use # instead of .(which used for class selectopr).

   $('#count').html(currentNum + ' of ' + count);

Modified code:

var currentNum = $(this).iniShow() + 1;
var count = $(this).closest('.slideshow').find('img').length;
       $('#count').html(currentNum + ' of ' + count);

Upvotes: 1

Sushanth --
Sushanth --

Reputation: 55740

You seem to be using this

.find('img:first').length;

So the count will always be 1 if it has images in it..

Try this instead

var count = $slides.closest('.slideshow').find('img').length;

Also this does not make any senses in the current function context.. Replace it with $slides

Upvotes: 4

Jen-Ya
Jen-Ya

Reputation: 318

The Syntax has to be:

var count = $('div.myDiv').find('img').size();

so in your case it would be:

var count = $(this).closest('.slideshow').find('img').size();

Upvotes: 0

Related Questions