Justin Meltzer
Justin Meltzer

Reputation: 13548

Using .children() to get an <img> element

How do you use the .children() jQuery method to get an image element? Is it .children('img')?

Upvotes: 0

Views: 7333

Answers (3)

mVChr
mVChr

Reputation: 50185

Maybe you should... http://jsfiddle.net/VvhCX/1/

Upvotes: 1

S W
S W

Reputation: 172

.children('img') can be used to get all children of an element with the tag img. See the documentation for more detail.

Upvotes: 0

Jon
Jon

Reputation: 437424

Yes, it is. For example:

<div id="foo">
    <img id="image1" src="blah.jpg" />
    <img id="image2" src="blah.jpg" />
    <img id="image2" src="blah.jpg" />
</div>

You can get the images by $("#foo").children("img") if the images are immediate children of the <div id="foo">.

Another, more concise way would be $("#foo > img") -- it's identical to the above.

If the images are descendants but not immediate children of the list, you can use $("#foo img").

Upvotes: 2

Related Questions