With jQuery Untils you easily traverse forward along next siblings, backwards along previous siblings, and upwards along parents, selecting all elements (or just elements that match an optional selector).

A simple parentsUntil example, with an "until" selector

Div with a class of "a"
Div with a class of "b"
Div with a class of "c" Span with a class of "d" Span with a class of "e" Span with a class of "f"

The code

$(function(){
  
  // Example 1: From .f, select all parent elements until .a
  $('#ex1 .f').parentsUntil('.a').addClass('selected');
  
});

A more complex parentsUntil example, with both an "until" and "each" selector

Div with a class of "a"
Div with a class of "b"
Div with a class of "c" Span with a class of "d" Span with a class of "e" Span with a class of "f"

The code

$(function(){
  
  // Example 2: From .f, select all parent 'div' elements until .a
  $('#ex2 .f').parentsUntil('.a', 'div').addClass('selected');
  
});