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

In this post we will show you How to get the children of the $(this) selector?, hear for How to get the children of the $(this) selector? we will give you demo and example for implement.

Html layout ::

<div id="DivId"><img src="ImgId"></div>

You would like to use a jQuery selector to select the child img inside the div on click.

Solution 1 : get 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);

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");

Solution 2 : get the children of the $(this) selector

If you need to get the first img that’s down exactly one level, you can do this follwoing jquery code

$(this).children("img:first")

Solution 3 : get the children of the $(this) selector

If your div tag is immediately followed by the img tag, you can do this follwoing jquery code

$(this).next();

Solution 4 : get the children of the $(this) selector

If you want direct children is of img tag, you can do this follwoing jquery code

$('> .child', this)

Solution 5 : get the children of the $(this) selector

jQuery’s each is one option for get the children of the $(this) selector :

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

$('#DivId img').each(function(){
    console.log($(this).attr('src'));
});

Hope this code and post will helped you for implement How to get the children of the $(this) selector. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org