0

I have a wrapper div containing 10 thumbnail divs each of which contains 10 img tags, I want an array of the first img tag's of each of these 10 thumbnail divs. Currently I am doing

$('#wrapper').find('.thumbnail').find('img:first') //--> [<img>, <img>, ... , <img>] 10 img tags total

Is there a shorter way to do this?

Thanks!

2 Answers 2

2
$('#wrapper .thumbnail > img:first-child')
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, forget my answer...this is better. :) I was going to suggest this one, when I saw Nik's comment about only the first img being returned. I didn't realized the difference between the first-child and first selectors..
This one did it! Thanks NeXXeus
1

Do it like this $('#wrapper .thumbnail img:first-child')

6 Comments

Thanks CarlosZ, but that returns only 1 img tag
Yes it is, make it first-child, and it should be working properly.
Thanks a lot @Aidiakapi, I just fixed it.
So does this differ from the answer from NeXXeus in that if I wrapped EACH of those img tag with, say, a div tag, this selector will still find what I want?
by using the #parent > .child selector you will only get the first-level descendants of #parent not using the > will allow to get all children with the .child class, regardless of whether they are first level.
|

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.