46 of 119 menu

The prev method

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

Syntax

Getting an immediate neighbor:

.prev();

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

.prev(selector);

Example

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

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

HTML the code will look like this:

<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
azbydeenesfrkakkptruuz