$(this) selector and children?

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?

ngoton Professor Asked on 17/02/2016 in Programmer.
Add Comment
  • 1 Answer(s)
    Best answer

    The jQuery constructor accepts a 2nd parameter called context which can be used to override the context of the selection.

    jQuery("img", this);

    Which is the same as using .find() like this:

    jQuery(this).find("img");

    If the imgs you desire are only direct descendants of the clicked element, you can also use .children():

    jQuery(this).children("img");
    ngoton Professor Answered on 17/02/2016.
    Add Comment

    Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.