Reputation: 478
wat is the jquery command to count the number of jpg image files in my document..
the command to count the image files:
$('#div').html($('img').length );
bt then this counts all the image files with the tag- img
wat is the command to count only the .jpg files..
I tried this
$('#div').html($("[img$='.jpg']").length);
but this does not gives the desired result. I searched for the same on net.. bt did not
find any working solution. can anyone pls help me with it? h
Upvotes: 0
Views: 362
Reputation: 9933
Something like this
var count = 0;
$('body').children('img').each(function() {
var patt = new RegExp('.jpg$');
var src = $(this).attr('src');
if (patt.test(src)) {
count++;
alert(count);
}
});
Heres a demo
Upvotes: 1
Reputation: 700302
Look at the src
attribute in the image tags:
$('#div').html($("img[src$='.jpg']").length)
Upvotes: 4
Reputation: 1074238
You can use an attribute ends with selector:
$('#div').html($('img[src$=".jpg"]').length);
// ^^^^^^^^^^^^^
Upvotes: 4