{"name":"find","type":"method","title":".find()","deprecated":null,"removed":null,"desc":"Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.","categories":["traversing/tree-traversal","version/1.0","version/1.6"],"entries":[{"return":"jQuery","signatures":[{"added":"1.0","argument":{"desc":"A string containing a selector expression to match elements against.","name":"selector","type":"Selector"}},{"added":"1.6","argument":{"desc":"A jQuery object to match elements against.","name":"jQuery object","type":"Object"}},{"added":"1.6","argument":{"desc":"An element to match elements against.","name":"element","type":"Element"}}],"examples":[{"desc":"Starts with all paragraphs and searches for descendant span elements, same as $( \"p span\" )","code":"\n$( \"p\" ).find( \"span\" ).css( \"color\", \"red\" );\n","html":"\n
Hello, how are you?
\nMe? I'm good.
\n"},{"desc":"A selection using a jQuery collection of all span tags. Only spans within p tags are changed to red while others are left blue.","css":"\n span {\n color: blue;\n }\n","code":"\nvar spans = $( \"span\" );\n$( \"p\" ).find( spans ).css( \"color\", \"red\" );\n","html":"\nHello, how are you?
\nMe? I'm good.
\n\n When the day is short\n find that which matters to you\n or stop believing\n
\n"}],"longdesc":"\nGiven a jQuery object that represents a set of DOM elements, the .find() method allows us to search through the descendants of these elements in the DOM tree and construct a new jQuery object from the matching elements. The .find() and .children() methods are similar, except that the latter only travels a single level down the DOM tree.
The first signature for the .find()method accepts a selector expression of the same type that we can pass to the $() function. The elements will be filtered by testing whether they match this selector.
Consider a page with a basic nested list on it:
\n\n<ul class=\"level-1\">\n <li class=\"item-i\">I</li>\n <li class=\"item-ii\">II\n <ul class=\"level-2\">\n <li class=\"item-a\">A</li>\n <li class=\"item-b\">B\n <ul class=\"level-3\">\n <li class=\"item-1\">1</li>\n <li class=\"item-2\">2</li>\n <li class=\"item-3\">3</li>\n </ul>\n </li>\n <li class=\"item-c\">C</li>\n </ul>\n </li>\n <li class=\"item-iii\">III</li>\n</ul>\n \n If we begin at item II, we can find list items within it:
\n\n$( \"li.item-ii\" ).find( \"li\" ).css( \"background-color\", \"red\" );\n \n The result of this call is a red background on items A, B, 1, 2, 3, and C. Even though item II matches the selector expression, it is not included in the results; only descendants are considered candidates for the match.
\nUnlike most of the tree traversal methods, the selector expression is required in a call to .find(). If we need to retrieve all of the descendant elements, we can pass in the universal selector '*' to accomplish this.
Selector context is implemented with the .find() method; therefore, $( \"li.item-ii\" ).find( \"li\" ) is equivalent to $( \"li\", \"li.item-ii\" ).
As of jQuery 1.6, we can also filter the selection with a given jQuery collection or element. With the same nested list as above, if we start with:
\n\nvar allListElements = $( \"li\" );\n \n And then pass this jQuery object to find:
\n\n$( \"li.item-ii\" ).find( allListElements );\n \n This will return a jQuery collection which contains only the list elements that are descendants of item II.
\nSimilarly, an element may also be passed to find:
\n\nvar item1 = $( \"li.item-1\" )[ 0 ];\n$( \"li.item-ii\" ).find( item1 ).css( \"background-color\", \"red\" );\n \n The result of this call would be a red background on item 1.
\n "}]}