jQuery API

Child Selector (“parent > child”)

child selector

version added: 1.0jQuery('parent > child')

  • parent
    Any valid selector.
    child
    A selector to filter the child elements.

Description: Selects all direct child elements specified by "child" of elements specified by "parent".

As a CSS selector, the child combinator is supported by all modern web browsers including Safari, Firefox, Opera, Chrome, and Internet Explorer 7 and above, but notably not by Internet Explorer versions 6 and below. However, in jQuery, this selector (along with all others) works across all supported browsers, including IE6.

The child combinator (E > F) can be thought of as a more specific form of the descendant combinator (E F) in that it selects only first-level descendants.

Example:

Places a border around all list items that are children of <ul class="topnav"> .

<!DOCTYPE html>
<html>
<head>
  <style>
body { font-size:14px; }
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
	
                    
<ul class="topnav">
    <li>Item 1</li>
    <li>Item 2 
        <ul><li>Nested item 1</li><li>Nested item 2</li><li>Nested item 3</li></ul>
       </li>
    <li>Item 3</li>
</ul>

<script>$("ul.topnav > li").css("border", "3px double red");</script>
</body>
</html>

Demo:

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for Child Selector (“parent > child”) below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.

2 Comments

  1. Ken
    Posted February 2, 2010 at 9:38 pm | Permalink

    Unless I'm missing something, the example label is incorrect “Finds all children of the element with id “main” which is yellow.”

  2. Greg
    Posted February 4, 2010 at 2:55 am | Permalink

    I think something is wrong as the example is not working? I don't see any red border around the LI tags. (using FF 3.5)

Post a Comment

You must be logged in to post a comment.