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

ID : 329

viewed : 189

Tags : javascriptjqueryjquery-selectorsjavascript





Top 5 Answer for javascript - How to get the children of the $(this) selector?

vote vote

92

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

88

You could also use

$(this).find('img'); 

which would return all imgs that are descendants of the div

vote vote

79

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

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

63

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

$(this).next(); 
vote vote

58

The direct children is

$('> .child-class', this) 

Top 3 video Explaining javascript - How to get the children of the $(this) selector?







Related QUESTION?