tags:

views:

301

answers:

3

Can I count the number of elements with a specific attribute?

For example all the images with the src attribute test.gif

+2  A: 

Use the CSS attribute selectors to filter on the attribute

$("img[src='test.gif']").length;

the size() and length both returns the number of elements matched;

chakrit
+2  A: 

You want size() so something like:

something like:

$('img[src="test.gif"]').size();
easement
`.length` is preferred because it is an attribute vs. a function call. In large loops, `length` will perform better.
Doug Neiner
A: 

if you want to check the presence of the attribute only

$('img[src]').length //or .size() 
Dave.Sol