cycling jQuery_Succinctly | Page 52

alert($('#start').parents()[2].id); // Gets actual DOM element. alert($('#start').closest('#parent2').attr('id')); })(jQuery); Notes: closest() and parents() might appear to have the same functionality, but closest() will actually include the currently selected element in its filtering. closest() stops traversing once it finds a match, whereas parents() gets all parents and then filters on your optional selector. Therefore, closest() can only return a maximum of one element. Traversing methods accept CSS expressions as optional arguments CSS expressions are not only passed to the jQuery function for selecting elements, but they can also be passed to several of the traversing methods. It might be easy to forget this because many of the traversing methods function without having to use any expression at all—e.g. next(). The expression is optional for the following traversal methods, but remember that you have the option of providing an expression for filtering.          children('expression') next('expression') nextAll('expression') parent('expression') parents('expression') prev('expression') prevAll('expression') siblings('expression') closest('expression') 52