javascript - with - jquery parent child selector

How to get the children of the $(this) selector? (11)

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:


How can I get the child img using a selector?

$(document).ready(function() {
  // When you click the DIV, you take it with "this"
  $('#my_div').click(function() {'Initializing the tests..');
    console.log('Method #1: '+$(this).children('img'));
    console.log('Method #2: '+$(this).find('img'));
    // Here, i'm selecting the first ocorrence of <IMG>
    console.log('Method #3: '+$(this).find('img:eq(0)'));
  background-color: yellow;
  width: 100%;
  height: 200px;
<script src=""></script>

<div id="my_div" class="the_div">
  <img src="...">

Also this should work:

$("#id img")

If you need to get the first img that's down exactly one level, you can do


If your DIV tag is immediately followed by the IMG tag, you can also use:


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:


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


Try this code:


Without knowing the ID of the DIV I think you could select the IMG like this:

$("#"+$(this).attr("id")+" img:first")

You can find all img element of parent div like below

$(this).find('img') or $(this).children('img')

If you want specific img element you can write like this

// where n is the child place in parent list start from 0 onwards

Your div contain only one img element. So for this below is right


But if your div contain more img element like below

<div class="mydiv">
    <img src="test.png" alt="3">
    <img src="test.png" alt="4">

then you can't use upper code to find alt value of second img element. So you can try this:


This example shows a general idea that how you can find actual object within parent object. You can use classes to differentiate your child object. That is easy and fun. i.e.

<div class="mydiv">
    <img class='first' src="test.png" alt="3">
    <img class='second' src="test.png" alt="4">

You can do this as below :


and more specific as:


You can use find or children as above code. For more visit Children and Find See example

You can use either of the following methods:

1 find():


2 children():


You could also use


which would return all imgs that are descendants of the div

jQuery's each is one option:

<div id="test">
    <img src="testing.png"/>
    <img src="testing1.png"/>

$('#test img').each(function(){