$(this) Selector And Children In jQuery
Here we consider how to use $(this) selector and children for finding elements in jQuery.
Lets start with one example here.
Suppose we have an image inside div as shown here.
 Selector And Children In jQuery 1.png)
When we click inside this Div, we have to fadeout and fadein the image.
This can be done in jquery by using a combination of $(this) and Children.
We can detect a click inside Div using jQuery Click event.
Inside Click event, to get the image, we can use Children or find.
$(function () {
$('#parentDiv').click(function(){
$(this).children('img').fadeOut().fadeIn();
})
});
$(function () {
$('#parentDiv').click(function(){
$(this).find('img').fadeOut().fadeIn();
})
});
Instead of children, we can make use of jQuery find as shown above.
By this way, we can use $(this) selector and children for finding elements in jQuery.