javascript tutorial - [Solved-4 Solutions] Get the children of the $(this) selector - javascript - java script - javascript array
Problem:
How can we get the child img using a selector ?
Solution 1:
The jQuery constructor accepts a 2nd parameter called context
jQuery("img", this);
click below button to copy the code. By JavaScript tutorial team
Which is the same as using .find()
like this:
jQuery(this).find("img");
click below button to copy the code. By JavaScript tutorial team
If the imgs we desire are only direct descendants of the clicked element, we can also use .children()
:
jQuery(this).children("img");
click below button to copy the code. By JavaScript tutorial team
Solution 2:
We could also use
$(this).find('img');
click below button to copy the code. By JavaScript tutorial team
which would return all imgs that are descendants of the div
Solution 3:
If we need to get the first img
that's down exactly one level, we can do
$(this).children("img:first")
click below button to copy the code. By JavaScript tutorial team
Solution 4:
If we DIV tag is immediately followed by the IMG tag, we can also use:
$(this).next();