When should we use .children() or .find()?

Question

In the context of this exercise, when should we use .children() or .find()?

Answer

.children() is used when you want to select descendant elements at just one level of nesting within an element. You can select all child elements regardless of their tags or attributes by calling the method without any arguments, or you can target specific children by providing some selector expression.

.find() is used when you want to search for a specific element at any level of nesting. Unlike .children() however, the .find() method always requires some selector expression as an argument, and cannot be empty.

One thing to note is that calling .children() without arguments selects all direct child elements regardless of tags or attributes, but there is no way to do this equivalently with the .find() method, since it is specific to the selector argument. To select all the direct children of an element using .find(), it may require several calls to the method.

4 Likes