15

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

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

3 Answers 3

20

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;

Sign up to request clarification or add additional context in comments.

Comments

6

if you want to check the presence of the attribute only

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

Comments

3

You want size() so something like:

something like:

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

1 Comment

.length is preferred because it is an attribute vs. a function call. In large loops, length will perform better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.