$() - Traversing DOM jQuery Demo

$(
	function(){
	
		// Loop over all of the LI elements.
		$( "li" ).each(
			function( intI, objLI ){
				var jLI = $( this );
				
				// Loop over each link to bind the click event with the
				// specific method of traversal.
				jLI.find( "a" ).each( 
					function( intJ, objLink ){
						var jLink = $( this );
						
						var strMethod = "prev";
						
						// Check to see which method we should use.
						if (intJ == 1){
							strMethod = "prevAll";
						} else if (intJ == 2){
							strMethod = "next";
						} else if (intJ == 3){
							strMethod = "nextAll";
						}
						
						// Set link href and bind click event.
						jLink
							.attr( "href", "javascript:void( 0 )" )
							.click(
								function( objEvent ){
									// Reset all highlight.
									$( "li.highlight" ).removeClass( "highlight" );
								
									// Execute the given method.
									// Ex: jLI.prev().addClass( "highlight" );
									jLI[ strMethod ]().addClass( "highlight" );
									
									// Prevent default click.
									return( false );
								}
								)
						;
						
					}
					);
				
			}
			);			
	}
	);