Use jQuery to select child element

Suppose you have the following structure:

<div class="section">
	<h3 class="title">Title</h3>
	<span class="description">...</span>
</div>
<div class="section">
	<h3 class="title">Title</h3>
	<span class="description">...</span>
</div>

This code will let you access the child elements of ‘.section’ when you use an ‘each’ function

<script type="text/javascript">
$(function(){
	$('.section').each(function(i,element){
		$('description',this).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
	});
});
</script>

I hope this helps someone, I kept forgetting how to do this, so this is mostly a reminder for myself.
Source: http://stackoverflow.com/questions/306583/jquery-this-selector-and-children/306904#306904

1 Comments Short URL , ,

One Response to “Use jQuery to select child element”

  1. Jorge Pedret February 2, 2011 at 8:41 pm #

    On a more recent version of jQuery I found that’s it’s easier to use the find() method:

    Using this article’s example, you can replace line 4 with this:

    $(this).find('description').fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);

Leave a Reply