I have a layout similar to this:
<div id="..."><img src="..."></div>
and would like to use a jQuery selector to select the child img
inside the div
on click.
To get the div
, I've got this selector:
$(this)
How can I get the child img
using a selector?
Here's a functional code, you can run it (it's a simple demonstration).
When you click the DIV you get the image from some different methods, in this situation "this" is the DIV.
Hope it helps!
If you need to get the first
img
that's down exactly one level, you can doThe jQuery constructor accepts a 2nd parameter called
context
which can be used to override the context of the selection.Which is the same as using
.find()
like this:If the imgs you desire are only direct descendants of the clicked element, you can also use
.children()
:Also this should work:
The direct children is
Ways to refer to a child in jQuery. I summarized it in the following jQuery: