usertest
usertest

Reputation: 27628

jquery count elements with attribute

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

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

Upvotes: 15

Views: 32957

Answers (3)

Dave.Sol
Dave.Sol

Reputation: 251

if you want to check the presence of the attribute only

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

Upvotes: 6

easement
easement

Reputation: 6139

You want size() so something like:

something like:

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

Upvotes: 3

chakrit
chakrit

Reputation: 61518

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;

Upvotes: 20

Related Questions