40 of 119 menu

Children's Method

The children method returns only the immediate children of an element and does not return text nodes. To return all children, including text and comments, use the contents method.

Syntax

Getting immediate descendants:

.children();

You can also filter descendants by a given selector:

.children(selector);

Example

Let's find all immediate descendants of the div tag and append '!' to them using the append method:

<div> <p>text</p> <p>text</p> <p>text</p> </div> $('div').children().append('!');

HTML the code will look like this:

<div> <p>text!</p> <p>text!</p> <p>text!</p> </div>

Example

Let's now find all immediate descendants with class www:

<div> <p class="www">text</p> <p class="www">text</p> <p>text</p> </div> $('div').children('.www').append('!');

HTML the code will look like this:

<div> <p class="www">text!</p> <p class="www">text!</p> <p>text</p> </div>

See also

  • method contents,
    which allows you to get the descendants of an element and the text inside that element
  • method parent,
    which allows you to get the parent of an element
  • method parents,
    which allows you to get all the parents of an element
  • JavaScript property children,
    which stores a pseudo-array of child elements
azbydeenesfrkakkptruuz