The prev method

The prev method returns the element that comes before the given one within the same parent.

Syntax

Getting an immediate neighbor:

javascript.prev();

You can also select an immediate neighbor only if it satisfies a given selector:

javascript.prev(selector);

Example

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

html<p>text</p>
<p>text</p>
<p id="test">text</p>
<p>text</p>
<p>text</p>
javascript$('#test').prev().html('!');

HTML the code will look like this:

html<p>text</p>
<p>!</p>
<p id="test">text</p>
<p>text</p>
<p>text</p>

See also

  • method next,
    which allows the element's neighbor below
  • method prevAll,
    which returns the elements that come before the given one within the same parent
  • 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
  • JavaScript property previousElementSibling, which contains the previous element located in the same parent