The prevAll method

The prevAll method returns all elements that come before the given one within a single parent.

Syntax

Getting all neighbors above:

javascript$(selector).prevAll();

You can also filter neighbors by a given selector:

javascript$(element).prevAll(selector);

Example

Let's find the element #test and set the text '!' to all its neighbors above using the html method:

html<p>outside</p>
<div>
  <p>inside</p>
  <p>inside</p>
  <p id="test">inside</p>
  <p>inside</p>
  <p>inside</p>
</div>
<p>outside</p>
javascript$('#test').prevAll().html('!');

HTML the code will look like this:

html<p>outside</p>
<div>
  <p>!</p>
  <p>!</p>
  <p id="test">inside</p>
  <p>inside</p>
  <p>inside</p>
</div>
<p>outside</p>

See also

  • method nextAll,
    which returns elements that are after the given one within the same parent
  • method prev,
    which allows the element's neighbor to be above
  • method prevUntil,
    which returns the elements that come before the given one
  • method siblings,
    which allows you to get the neighbors of an element within one parent