Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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?

1 Answer

0 votes
by (106k points)
edited by

For getting the children of the $(this) selector the jQuery constructor accepts a 2nd parameter called context which can be used to override the context of the selection.

jQuery("img", this);

The above one is the same as using .find()method:

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

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

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

Are you willing to pursue a career in Web Development, here's an opportunity for you Web Development Certification provided by intellipaat! 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Aug 13, 2019 in Web Technology by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...