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 prevUntil example, with an "until" selector

  1. List item with a class of "a"
  2. List item with a class of "b"
  3. List item with a class of "c"
  4. List item with a class of "d"
  5. List item with a class of "e"
  6. List item with a class of "f"

The code

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

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

Paragraph with a class of "a"

Span with a class of "b"

Paragraph with a class of "c"

Span with a class of "d"

Paragraph with a class of "e"

Span with a class of "f"

The code

$(function(){
  
  // Example 2: From .f, select all previous sibling 'p' elements until .a
  $('#ex2 .f').prevUntil('.a', 'p').addClass('selected');
  
});