In this post, we are going to discuss about an important jQuery traversing method to find all the descendents of an element. jQuery API provides find() method to carry out this task.

jQuery find() method

The jQuery find() method returns all the descendant elements like child, grandchild and so on. This method is used if you want to search all the elements deeper in the DOM tree. This method traverses downwards and finds all its descendants of the selected element. This method takes a mandatory filtering parameter to narrow down the search.

Here is the general syntax for using find() method:

filter is the mandatory parameter passed to the find() method. filter could be any string selector expression, an element or a jQuery object.

jQuery find() example

Following example demonstrates the jQuery find() method usage.

In this example, you can see two p elements, one element is div element’s child and the other one is it’s grandchild. The find() method finds all the p element of the selected div element and changes the color to red. Below image shows the output produced by above HTML page.

jquery-find-example-450x405

You can use selector.find("*") to find all the descendants of the HTML document.

That’s all for now and you can find more traversing methods in the coming posts.

By admin

Leave a Reply