A demonstration of some common jQuery tree traversal methods.
As you can see, the buttons have the jQuery code that will be executed when clicking on the button. The div element with the ID of target will be the starting point for all of the demos. The flag class is simply used as a demo class to show off the prevUntil and nextUntil methods.
Next and previous
prev() and next() move to either the prior or next element, respectively.
prevAll and nextAll
prevAll() and nextAll() will select all siblings (elements on the same level of the DOM hierarchy) prior to or after the current element, respectively.
prevUntil and nextUntil
prevUntil and nextUntil will select all elements before or after the current element, upto but not including the element that matches the selector passed into the method.
HTML
You can review the jQuery used to create this demo. You will notice that each click event handler has a call to a helper function to clear the highlight. This exists because there is no guaranteed order of event handler execution, so there isn't a way to ensure the highlight class is removed before it's added onto the appropriate items.
It's also worth highlighting that you could make all of the buttons dynamic, reading the text on each button and passing tha value into jQuery. I didn't want to do that as I wanted to show the actual jQuery syntax in the source code.
Speaking of code cleanup, you will also notice the displayHtml helper function, which maintains the sample HTML while also displaying it on the page. This was created as a way to show the HTML and make the demonstration easier to understand. There are other third party plugins that can do things like that, and they probably use cleaner code than I wrote. ;-) My goal was to simply provide a nice little interactive demo.
I have offloaded the CSS and utility JavaScript to this pen .